Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add custom routes during compilation passes?

I prepare external bundle and I would like to add some routes during compilation passes. Routes will be created on the main app/config/config.yml settings.

I was trying to get router from ContainerBuilder in my CustomCompilerPass via:

$definition = $container->getDefinition('router');

, but I got The service definition "router" does not exist.

Is it possible to add custom routes during compilation passes?

like image 254
NHG Avatar asked Oct 21 '22 01:10

NHG


1 Answers

There's no way to add routes at compiler passes.
In order to dynamicly load routes (aware of container parameters) I'd use a custom route loader as given in my previous example

class MyLoader extends Loader
{
    protected $params;

    public function __construct($params)
    {
        $this->params = $params;
    }

    public function supports($resource, $type = null)
    {
        return $type === 'custom' && $this->params == '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/dynamic_routing.yml';
        $type     = 'yaml';

        $routes->addCollection($this->import($resource, $type));

        return $routes;
    }
}

routing.yml

_custom_routes:
    resource: .
    type:     custom
like image 134
Touki Avatar answered Oct 25 '22 18:10

Touki