Zend 路由冲突
我定义了 2 条自定义路线。一个用于threads/:id/:name
,另一个用于threads/tags/:tagName
,但是第二个与第一个冲突,因为如果我同时启用两个,则第一个会中断并将 :id
字面地视为一个操作,而不遵守 \d+
要求(我也尝试使用纯正则表达式路由,请参见底部)。
操作“1”不存在且不存在 陷入 __call()
我尝试重新安排路线的顺序,但如果我这样做,那么 threads/tags/:tagName
无法正确捕获 tagName。
我还尝试禁用默认路由,但此后路由仍然无法正常工作。
这是我的路由初始化函数:
protected function _initRoutes() {
$fc = Zend_Controller_Front::getInstance();
$router = $fc->getRouter();
$router->addRoute(
'threads',
new Zend_Controller_Router_Route('threads/:id/:name',
array(
'controller' => 'threads',
'action' => 'thread',
),
array(
'id' => '\d+'
)
)
);
$router->addRoute(
'threads',
new Zend_Controller_Router_Route('threads/tags/:tagName',
array(
'controller' => 'threads',
'action' => 'tags',
),
array(
'tagName' => '[a-zA-Z]+'
)
)
);
}
我也尝试使用纯正则表达式路由但不成功,很可能是因为我做错了:
$router->addRoute(
'threads',
new Zend_Controller_Router_Route_Regex(
'threads/(\d+)/([a-zA-Z]+)',
array(
'controller' => 'threads',
'action' => 'thread',
),
array(
1 => 'tagName',
2 => 'name'
)
)
);
I have defined 2 custom routes. One for threads/:id/:name
and the other for threads/tags/:tagName
however the second one conflicts with the first because if I enable both then the first breaks and treats :id
literally as an action, not obeying the \d+
requirement ( I also tried using pure regex routes, see bottom ).
Action "1" does not exist and was not
trapped in __call()
I tried re-arranging the order of the routes but if I do that then the threads/tags/:tagName
doesnt correctly capture the tagName.
I also tried disabling default routes but the routes still don't properly work after that.
Here's my route init function:
protected function _initRoutes() {
$fc = Zend_Controller_Front::getInstance();
$router = $fc->getRouter();
$router->addRoute(
'threads',
new Zend_Controller_Router_Route('threads/:id/:name',
array(
'controller' => 'threads',
'action' => 'thread',
),
array(
'id' => '\d+'
)
)
);
$router->addRoute(
'threads',
new Zend_Controller_Router_Route('threads/tags/:tagName',
array(
'controller' => 'threads',
'action' => 'tags',
),
array(
'tagName' => '[a-zA-Z]+'
)
)
);
}
I also tried using a pure regex route but was unsuccessful, most likely because I did it wrong:
$router->addRoute(
'threads',
new Zend_Controller_Router_Route_Regex(
'threads/(\d+)/([a-zA-Z]+)',
array(
'controller' => 'threads',
'action' => 'thread',
),
array(
1 => 'tagName',
2 => 'name'
)
)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。
啊,我真傻。 addRoute 的第一个参数需要是唯一的名称,并且并不像我假设的那样直接对应于控制器。
感谢#zftalk 上的 d__asmoka、lutinvert。我会尽快接受(最短 2 天)。
Solved.
Ah, silly me. The first argument to addRoute needs to be a unique name, and doesn't correspond directly to the controller as I assumed.
Thanks to d__asmoka, lutinvert on #zftalk. I'll accept this as soon as I can ( minimum is 2 days ).