【发布时间】:2013-07-24 01:33:50
【问题描述】:
是否可以在一个定义中定义同一个控制器的多个路由?
例如:
我想要一个单一的定义
/, /about, /privacy-policy
使用类似的东西
_home:
pattern: {/ , /about, /privacy-policy}
defaults: { _controller: AcmeDemoBundle:Home:index, about, privacy_policy }
我不想按照here 的建议在单独的定义中定义多个路由。
编辑:这是我的源代码:
<?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');
}
}
这是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
【问题讨论】:
-
如何按照您发布的链接中的建议使用注释?这是 AFAIK 的唯一方法......
-
我尝试使用它,但出现错误“找不到“GET /about”的路线”,你能告诉我我错过了什么吗?即使对于我的另一个方法“索引”,我也收到错误“[语义错误] Acme\DemoBundle\Controller\HomeController::indexAction() 方法中的注释“@Route”从未导入。你可能忘记添加“使用"这个注释的声明?"
-
它是名为SensioFrameworkExtraBundle 的可选捆绑包的一部分,此特定功能的配置可在此处找到:symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/…(不要忘记控制器开头的
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;语句) -
我已经添加了我的代码,在使用额外的路由控制器后 /about 页面仍然无法正常工作并出现相同的错误“找不到 "GET /about" 的路由”
-
你可以有多个路径指向同一个 Controller:Action。但是,不能有相同的路径指向不同的 Controllers:Actions。所以你应该删除
indexAction()或aboutAction()上的@Route("/")。你有activated the annotations in yourrouting.yml吗?
标签: symfony routing definition