Kofel
Kofel

Reputation: 53

Symfony 2 dynamically load routes

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?

Upvotes: 4

Views: 2874

Answers (2)

Igor Pantović
Igor Pantović

Reputation: 9246

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.

Upvotes: 4

Dan Morphis
Dan Morphis

Reputation: 1727

If you do a true bundle approach for each module, then the easiest way to accomplish what your trying to do is use the JMS Security-Extra bundle with attribute-based routing.

To your composer.json file, add this: "require": { ... "jms/security-extra-bundle": "1.5.*",

Update your composer file with

php composer.phar update

Then in your BundleName/Resources/config/routing.yml file do this:

some_name:
    type:     annotation
    resource: "@SomeBundle/Controller"

Finally, for each action in your controller, decorate it with @Route attributes:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
* @Route("/SomeBundle/SomeController")
*/
class SomeController extends Controller {
    /**
    * @Route("someAction", name="myAction")
    * @Method("GET") OR
    * @Method({"GET", "POST"})
    */
    public function someAction() {
    }
}

Some of the other attributes in the JMS bundle make things really nice as well. For example, I use the @Template attribute on my actions quite a bit. This means that I no longer have to do:

public function recentListAction() {
    ...
    return $this->render(
        'AcmeArticleBundle:Article:recentList.html.twig',
        array('articles' => $articles)
    );
}

I can simply do:

/**
* @Route("/Articles/List")
* @Template()
*/
public function recentListAction() {
    ...
    return array('articles' => $articles);
}

And as long as I have a Resources/views/ControllerName/recentList.html.twig file, everything will be weaved together for me automatically.

Upvotes: 0

Related Questions