Zend 和 addRoute() 的问题
使用此代码:
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'/test/:action/:type/:id',
array(
'controller' => 'admin'
)
)
);
http://app/test/param1/param2/param3 ->确定
http://app/test/param1/param2/ ->失败
在第二种情况下,应用程序无法识别 param2。
似乎应用程序需要 param3 才能读取 param2...
我该怎么做?
谢谢!
中的代码进行测试
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'/test/:action/:type/:id',
array(
'controller' => 'admin',
'id' => 0
),
array(
'id' => '\d+'
)
)
);
使用 @RageZ http://app/test/ -> 好的
http://app/test/some ->好的
http://app/test/some/more ->失败
http://app/test/some/more/andmore ->好
主意吗?
With this code:
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'/test/:action/:type/:id',
array(
'controller' => 'admin'
)
)
);
http://app/test/param1/param2/param3 -> OK
http://app/test/param1/param2/ -> FAIL
In the second case the application don't recognize param2.
It seems that the application needs the param3 in order to read the param2...
How can I do it?
Thanks!
Test with the code from @RageZ
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'/test/:action/:type/:id',
array(
'controller' => 'admin',
'id' => 0
),
array(
'id' => '\d+'
)
)
);
http://app/test/ -> OK
http://app/test/some -> OK
http://app/test/some/more -> FAIL
http://app/test/some/more/andmore -> OK
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试为所有内容指定默认值
刚刚在一些测试项目中尝试了您的代码,这修复了它希望它对您有用!
try giving a default value to everything
Just tried your code in some test project and this fixed it hope it works for you!
如果参数是可选的,则必须提供默认值。
与您的问题无关,但使用 addRoute 的第三个参数是一个很好的做法。 Zend Framework 将验证参数值是否与您指定的格式匹配,在这种情况下,我认为 id 是一个整数。
You have to provide a default value if the parameter is optional.
Nothing to do with your question but it's a good practice to use the third param of addRoute. Zend Framework would verify that the parameters value match the format you have specified, in that case I suppose id is an integer.