Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 dynamically load routes

Tags:

php

symfony

I'm going to create a modules system in my Symfony 2 app. Each module will be a bundle.

I don't know how to I can dynamically (in my service code) load routes from file (eg. AcmeSomeModuleBundle/Resources/config/routing.yml) and apply them with some prefix (or host). Like it's done by embedding code below in app/config/routing.yml:

berg_applications:
    resource: "@AcmeSomeModuleBundle/Resources/config/routing.yml"
    host: foobar.com

Any solutions?

like image 308
Kofel Avatar asked Oct 24 '25 10:10

Kofel


1 Answers

You need custom route loader IMO: http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html

For one project, I had to build route loader which loaded routes by fetching them from remote URL via CURL and it worked perfectly.

Documentation is very clear and it's silly easy to build one yourself when you look at the example. Basically, key things are:

  • "type" when you're defining a route resource. You should make your custom type so that your route loader recognizes it and takes it for processing.
  • ::load() method.

If you have any concrete problems you stumble upon don't hesitate to post question in comment. Basically, your RouteLoader will receive "resource" in it's load method and should do whatever it needs to do with it to add new Route to Router.

like image 197
Igor Pantović Avatar answered Oct 26 '25 22:10

Igor Pantović