Symfony 2: route defined in annotation not visible from by Twig's path()

13,575

Routes by annotations still need to be imported into routing.yml as so:

AcmeHelloBundle:
  resource: "@AcmeHelloBundle/Controller"
  type: annotation

This will tell the routing to scan the Controller directory of the Acme\HelloBundle and import all routes.

You can find more information about routing with annotations here. That link will also tell you how to activate routes as I have shown above.

Also, I noticed that your route annotation needs the name parameter to be accessible through register using the path function otherwise it'd be accessed through acme_bundlename_controllername_actionname:

@Route("/register", name="register")

Hope that helps!

Share:
13,575
mkrowiarz
Author by

mkrowiarz

Updated on June 05, 2022

Comments

  • mkrowiarz
    mkrowiarz almost 2 years

    I encountered a problem, have the following:

    DefaultController with a simple action:

    /**
     * @Route("/register")
     * @Template
     */
    public function indexAction() {
        $oForm = $this->createForm(new RegisterType());
        return array(
            'form'  => $oForm->createView()
        );
    }
    

    In my twig template I try to use:

    <form action="{{ path('register') }}" method="post"></form>
    

    But I get the following error:

    An exception has been thrown during the rendering of a template ("Route "register" does not exist.") in EBTSCustomerBundle:Default:index.html.twig at line 2.
    

    When I explicitely define a "register" route in app/config/routing.yml:

    register:
      pattern:  /register
      defaults: { _controller: EBTSCustomerBundle:Controller:Default:index }
    

    Then it works fine. Can't find any reasonable docs about it, I thought that routes defined via annotations should be visible in the whole application.

    Any ideas guys?

  • mkrowiarz
    mkrowiarz over 12 years
    Sorry, I missed that import in my previous note, routes from my bundle were imported just like you wrote (resource: @AcmeHelloBundle...), but the missing name="register" part in my annotation was causing the problem. Thanks for your input, it really helped!
  • Mike
    Mike about 12 years
  • Cavachon
    Cavachon over 11 years
    Had the same problem...turned out my "name" in the annotation was set incorrectly. Thanks.