简体   繁体   中英

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

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM