4

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?

5
  • This could help you : symfony.com/doc/current/cmf/book/routing.html#the-dynamicrouter Commented Oct 28, 2013 at 16:41
  • Well, I think it's not very helpful for me :/ Commented Oct 28, 2013 at 16:50
  • Why can't you do it the way the Symfony manual shows? Commented Oct 28, 2013 at 16:51
  • Because, I need to load this routes from .yml file, not define myself with PHP Code. Commented Oct 28, 2013 at 16:57
  • Why do you "need" to load the routes from a yaml file ? PHP loads yaml files... So that should not be a problem Commented Oct 28, 2013 at 20:06

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.