2

Is it possible to define multiple routes of same controller in a single definition?

For Example:

I want to have a single definition for

/, /about, /privacy-policy

using something like

_home:    
    pattern:   {/ , /about, /privacy-policy}  
    defaults:  { _controller: AcmeDemoBundle:Home:index, about, privacy_policy }

I don't want to define multiple routes in separate definition as suggested here.

EDIT: This is my source code:

<?php

namespace Acme\DemoBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HomeController extends Controller
{
    /**
     * @Route("/")
     */
    public function indexAction()
    {
        /*
         * The action's view can be rendered using render() method
         * or @Template annotation as demonstrated in DemoController.
         *
         */
        return $this->render('AcmeDemoBundle:Home:home.html.tpl');
    }
    /**
     * @Route("/about")
     */
    public function aboutAction()
    {
        return $this->render('AcmeDemoBundle:Home:about.html.tpl');
    }    
}

This is the source code of routing.yml

_home:
    pattern:  /
    defaults: { _controller: AcmeDemoBundle:Home:index }
_welcome:
    pattern:  /
    defaults: { _controller: AcmeDemoBundle:Welcome:index }
_demo_secured:
    resource: "@AcmeDemoBundle/Controller/SecuredController.php"
    type:     annotation

_demo:
    resource: "@AcmeDemoBundle/Controller/DemoController.php"
    type:     annotation
    prefix:   /demo
4

2 回答 2

4

如果你没有为你的路由注释声明名字,symfony 会生成一个。(“使用 @Route 注释定义的路由被赋予一个由包名称、控制器名称和操作名称组成的默认名称。”来自http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing .html )

通过声明多个路由 symfony 生成一个名称(如果您不精确命名),女巫可能会通过保留第一个来覆盖您的其他路由。

您可以尝试通过为注释中的每条路线随机命名来进行测试。

最后一件事是不要忘记你的根路由文件,方法是添加一个链接到你的包,就像文档中的这个例子一样。

blog:
    resource: "@SensioBlogBundle/Controller"
    type:     annotation 
于 2013-07-24T14:40:26.643 回答
2

[语义错误] 从未导入方法 Debril\RssAtomBundle\Controller\YOURController::indexAction() 中的注释“@Template”。您是否可能忘记为此注释添加“使用”语句?

必须在 YOURController 中添加两个用途:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
于 2015-10-29T23:38:35.280 回答