Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 961
mkrowiarz Avatar asked Dec 06 '11 20:12

mkrowiarz


Video Answer


1 Answers

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!

like image 197
Kasheen Avatar answered Oct 17 '22 23:10

Kasheen