Enable routes based on container settings in Symfony 2.3

I created a package that has its own routing.yml. Instead of just importing these routes in my main routing.yml application, I would like to import them only if a specific parameter is set in the service container. This way I can enable or disable routes based on the package configuration. Is there any way to do this, except by creating a new environment?

The reason I want to do this is because I need some routes for a special stress testing installation that should never be included in other deployments.

+1
source share
1 answer

You can dynamically create routes by creating your own Route Loader

Announce the service first

services.yml

services: acme_foo.route_loader: class: Acme\FooBundle\Loader\MyLoader arguments: - %my.parameter% tags: - { name: routing.loader } 

Then create a class

Acme \ FooBundle \ Loader \ MyLoader

 use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Config\Loader\Loader; class MyLoader extends Loader { protected $params; public function __construct($params) { $this->params = $params; } public function supports($resource, $type = null) { return $type === 'custom' && $this->param == 'YourLogic'; } public function load($resource, $type = null) { // This method will only be called if it suits the parameters $routes = new RouteCollection; $resource = '@AcmeFooBundle/Resources/config/custom_routing.yml'; $type = 'yaml'; $routes->addCollection($this->import($resource, $type)); return $routes; } } 

Then just add import to your routing

app / config / routing.yml

 _custom_routes: resource: . type: custom 
+1
source

Source: https://habr.com/ru/post/1500567/


All Articles