使用可选参数配置Silex路由

时间:2013-07-26 12:35:30

标签: routing silex

我配置了这两条路线

$app->match ( '/controller/param/{country}/{q}', "demo.controller:demoAction" )->value ( 'country', 'de' )->value('q', '')->bind ( 'demo_action_with_country' );
$app->match ( '/controller/param/{q}', "demo.controller:demoAction" )->value ( 'q', '' )->bind ( 'demo_action_without_country' );

然而 - 这不起作用。如果我呼叫/controller/param/Test-String路由匹配并返回内容。如果我打电话给/controller/param/DE/Test-String,我会得到 NotFoundHttpException

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:6)

好的,我可以通过我自己轻松解决这个问题。这是我的解决方案:

$app->match ( '/controller/param/{q}', "demo.controller:demoAction" )->value ( 'q', false )->bind ( 'demo_action_without_country' );
$app->match ( '/controller/param/{country}/{q}', "demo.controller:demoAction" )->value ( 'country', false )->value('q', '')->bind ( 'demo_action_with_country' );