【发布时间】:2013-06-08 05:33:27
【问题描述】:
在我的 Symfony2 应用程序中,我想用一个路由创建四个 url:
- a-lot-of-other-stuff/report/-20 (负数)
- a-lot-of-other-stuff/report/40 (正数)
- 很多其他东西/报告/ (无编号)
- 很多其他的东西/报告(没有数字和没有/)
我的路线目前是这样的:
report:
pattern: /report/{days}
defaults: { _controller: "AppReportBundle:Report:dayReport", days = null }
动作定义为:
public function dayReportAction($days = null)
{
// my code here
}
这目前使 url 1 和 2 工作,但在 url 3 和 4 的情况下,我得到一个错误
找不到路线
如何使参数“天”成为可选参数?
如果没有提供参数,我怎么能让/也被省略?
【问题讨论】:
-
这很奇怪。因为第四条路线应该有效。我经常使用可选参数,但我使用 Annotation Route 定义。如果我在函数签名中定义默认值,我不应该在注释中定义默认值。也许您需要省略默认声明之一(例如在您的 routing.yml 中)或尝试使用数字(或字符串/布尔)值而不是 null。也许会有所帮助。
-
你的路由配置应该是:
defaults: { _controller: "AppReportBundle:Report:dayReport", days: null }