是否可以在單個定義中定義多個相同控制器的路由?Symfony2多路由單控制器
例如:
我想有使用的東西
/, /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唯一的方法... – cheesemacfly
我試過使用,但得到錯誤「找不到路由」GET/about「」,你能告訴我我錯過了什麼嗎?即使對於我的另一種方法「索引」,我也得到錯誤「[語義錯誤] Acme \ DemoBundle \ Controller \ HomeController :: indexAction()方法中的註釋」@Route「從未導入。您是否忘記添加」use 「這個註釋的聲明?」 – neeraj
它是名爲[SensioFrameworkExtraBundle](http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html)的可選包的一部分,此特定功能的配置可在此處找到:http:// symfony .com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html(不要忘記在你的控制器的開頭使用'Sensio \ Bundle \ FrameworkExtraBundle \ Configuration \ Route;'語句) – cheesemacfly