user2559108
user2559108

Reputation:

Symfony2 GenerateUrl with annotation

For some reason, i cannot generate the route i have specified for my form builder.

As you see, i specify generateUrl('/login'), but, during form building, the error "Unable to generate a URL for the named route "/login" as such route does not exist. ".

Route /login is declared below using annotation, what seems to be the issue? my App controller is set to use annotation.

/**
 * @Route("/")
 * @Template()
 */
public function indexAction() {

    $user = new User();

    $form = $this->createFormBuilder($user)
            ->setAction($this->generateUrl('/login'))
            ->setMethod('POST')
            ->add('username', 'text')
            ->add('password', 'text')
            ->add('submit', 'submit')
            ->getForm();

    $content = $this->renderView('ThisBundleBundle:Login:index.html.twig', array(
        'form' => $form->createView(),
    ));

    return new Response($content);
}

/**
 * @Route("/login")
 * @Template()
 */
public function loginAction() {



}

routing.yml in the app:

bill_ThisBundleBundle: resource: "@ThisBundleBundle/Controller/" type: annotation prefix: /

Upvotes: 1

Views: 785

Answers (1)

Alexey B.
Alexey B.

Reputation: 12033

You must specify the name of route like

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

and then generate url $this->generateUrl('some_route_name'). Or find your route with command php app/console router:debug and see internal route name.

Upvotes: 3

Related Questions