我正在学习symfony书,我已经读到我可以省略控制器中的参数(http://symfony.com/doc/2.3/book/controller.html#route-参数作为控制器参数)
我已经尝试过这种配置
我的路线
_demo:
path: /{lastName}
defaults: { _controller: AcmeDemoBundle:Demo:index}
和我的控制器
public function indexAction(){
return $this->render('AcmeDemoBundle:Demo:index.html.twig');
}
当我访问没有参数的路线(lastName)时
http://localhost:8000/app_dev.php/
我收到这个错误
No route found for "GET /"
但在符号书中,我可以阅读:
并非所有路由参数都需要是控制器上的参数
例如,如果lastName对控制器不重要,你可以完全省略它:
我使用的是symfony 2.3非常感谢!
现在您没有带有/
的路由,而是带有参数/{param}
的路由
如果您访问
http://localhost:8000/app_dev.php/foo
你可以得到这个路线,你可以省略你的控制器中的参数
public function indexAction()
{
return $this->render('AcmeDemoBundle:Demo:index.html.twig');
}
参数在控制器中是可选的,而不是在路线中。然后,如果你想要/
路由,你必须创建一个新的路由。
E.g
_demo2:
path: /
defaults: { _controller: AcmeDemoBundle:Demo:index2}
你可以用检查你的路线
php app/console router:debug
您可以读取所有路线
为了让您的路由从控制器中达到indexAction,您需要为lastName
:指定一个默认值
_demo:
path: /{lastName}
defaults: { _controller: AcmeDemoBundle:Demo:index, lastName: null}