Custom Route Configuration with Silex

I know that the basis of the Silex approach, in which all the application logic is in a single file. But I will have the opportunity to have more than twenty controllers. Therefore, I want to have a convenient card for controlling the router.

My question is finding solutions in which I could make a router for a single file. In the best case, the file should be of type YAML:

# config/routing.yml

_home:
    pattern: /
    defaults: { _controller: MyProject\Controller\MyController::index }

But native is also a good case (for me):

$routes = new RouteCollection();

$routes->add(
    'home',
    new Route('/', array('controller' => 'MyProject\Controller\MyController::index')
));

return $routes;

The problem of the second case is that I have to use the match () function for each routing rule. This is not entirely clear.

What are the ways to solve this problem? The condition is that I want to use the existing Silex API or Symfony2 components.

A small note:

ControllerProviderInterface . .

+5
4

, Silex , . Silex , "", .

" silex, Symfony2, ". -

blogpost, Silex.


Yaml pattern defaults._controller .

Yaml, Symfony2. , Silex:

// parse the yaml file
$routes = ...;
$app = new Silex\Application();

foreach ($routes as $route) {
    $app->match($route['pattern'], $route['defaults']['_controller']);
}

// ...
$app->run();
+10

, , , . FileLocator/YamlFileLoader , , / yaml.

Composer

-, . Symfony YAML , Silex.

"require": {
    "symfony/yaml": "~2.3",
    "igorw/config-service-provider": "1.2.*"
}

, (routes.yml):

config.routes:
  dashboard:
    pattern:  /
    defaults: { _controller: 'IndexController::indexAction' }
    method:   GET

yaml. - , $app ( ).

$this->register(new ConfigServiceProvider(__DIR__."/../config/services.yml"));
$this->register(new ConfigServiceProvider(__DIR__."/../config/routes.yml"));
// any more yaml files you like

, :

$routes = $app['config.routes']; // See the first key in the yaml file for this name
foreach ($routes as $name => $route)
{
    $app->match($route['pattern'], $route['defaults']['_controller'])->bind($name)->method(isset($route['method'])?$route['method']:'GET');    
}

->bind() "" URL-, , , .
->method() POST | . , "GET" , .

+5

, .

run():

# /src/Application.php

...

protected function _initRoutes()
{
    $locator = new FileLocator(__DIR__.'/config');
    $loader = new YamlFileLoader($locator);

    $this['routes'] = $loader->load('routes.yml');
}

- , Silex\Application.

Configuration file:

# /src/config/routes.yml
home:
    pattern: /
    defaults: { _controller: '\MyDemoSite\Controllers\DefaultController::indexAction' }

It works great for me!

UPD

I think this is the right option for adding collections:

$this['routes']->addCollection($loader->load('routes.yml'));

More flexible.

0
source

You can expand the service routes(which is RouteCollection) and download the YAML file with FileLocatorand YamlFileLoader:

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;

$app->extend('routes', function($routeCollection) {

    $locator    = new FileLocator([__DIR__ . '/../config']);
    $loader     = new YamlFileLoader($locator);
    $collection = $loader->load('routes.yml');

    $routeCollection->addCollection($collection);

    return $routeCollection;

});

However, you will symfony/configalso need symfony/yamldependencies.

0
source

All Articles