Symfony/Twig how to render a Route set by anotation?

684 Views Asked by At

Let's say I have this code in a controller:

<?php

namespace Foo\BarBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Mvc;

/**
 * @Mvc\Route("/foo/bar")
 */
class TestController extends Controller
    /**
     * @Mvc\Route("/test/{id}", requirements={"id" = "[0-9]{1,6}"})
     * @Mvc\Template
     *
     * @return view
     */
    public function testAction($id)
    {
        return array('test' => $id);
    }
}

How can I link to this route in a twig template? Usually I can just call

{{ path('route_name', {'paramkey': 'paramvalue'}) }}

But here I have no name to call. Same thing, how would I call it in a controller (for a redirect)?

Thanks.

1

There are 1 best solutions below

1
On BEST ANSWER

When you omit the name it's being autogenerated for you.

The autogenerated name is a lowercase concatenation of bundle + controller + action. For example, if you have:

  • Bundle AppBundle
  • Controller MyController
  • Action: testAction()

the name would be app_my_test.

You can list all routes using Terminal:

php app/console router:debug

All routes' names, including those autogenerated, will be there.

Hope this helps...