Zend 路由器行为
我的路由器有一些问题。
我有一个自定义路线:
$router->addRoute('showTopic',
new Zend_Controller_Router_Route('/forum/topic/:topic',
array('module' => 'forum',
'controller' => 'topic',
'action' => 'show'),
array('topic' => '\d+')));
但是当我尝试访问此网址时:localhost/forum/topic/16 我收到此错误:
致命错误:未捕获异常“Zend_Controller_Router_Exception”,消息“未指定主题”
但我不想为主题设置默认值,因为我还希望路由 /forum/topic 列出所有主题。 ..
其次,我知道如果添加自定义路由,默认路由器将被覆盖,但我也需要一些默认路由。我发现的唯一方法是在 url 视图助手的第二个参数中设置“默认”,就像这样
$this->url(array(
'module' => 'forum',
'controller' => 'topic',
'action' => 'add'
), 'default', true)
有没有更优雅的方法而不是对我想使用默认行为的所有 url 执行此操作?
I have some trouble with the router.
I have a custom route :
$router->addRoute('showTopic',
new Zend_Controller_Router_Route('/forum/topic/:topic',
array('module' => 'forum',
'controller' => 'topic',
'action' => 'show'),
array('topic' => '\d+')));
But when I try to access this url : localhost/forum/topic/16
I get this error :
Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'topic is not specified'
But I don't want to put a default value for topic, because I also want the route /forum/topic to list all topics...
Secondly, I know that if I add a custom route, the default router is overridden, but I need to have some default routes too. The only way I have found is to set 'default' in the second parameter of the url view helper, like this
$this->url(array(
'module' => 'forum',
'controller' => 'topic',
'action' => 'add'
), 'default', true)
Is there a more elegant way instead of doing this for all url where I want to use the default behavior ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该为某个主题设置一个默认值,并在更具体的路由之后添加更通用的路由(论坛/主题的路由)。 Route_Rewrite 检查从最后一个开始的路由(它实际上执行 array_inverse)。
url 帮助器将程序集 url 委托给路由,其第二个参数是要从路由器拉取的路由的名称。由于默认路由是以“default”名称注册的,因此使用该名称并没有什么不雅的地方(它不是魔术字符串或特殊情况)。如果这确实让您烦恼,您可以编写一个自定义帮助程序(放置在“views/helpers”下):
并在您的视图中使用它,例如defaultUrl(array('action'=>'test')) ?>。
You should have a default value for a topic and add the more general route (the one for forum/topic) after the more specific one. Route_Rewrite checks the routes beginning with the last one (it actually does an array_inverse).
The url helper delegates assembly urls to a route, its second paremeter being the name of the route to pull from the router. Since the default route is registered under the name of 'default', there is nothing really inelegant in using the name (it is not a magic string or a special case). If this really bugs you, you could write a custom helper (to be placed under "views/helpers"):
And use it in your view like defaultUrl(array('action'=>'test')) ?>.