开发者

Zend Routes conflict

开发者 https://www.devze.com 2022-12-16 23:18 出处:网络
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 tre

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 ro开发者_开发问答utes, 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'
    )
    )
);


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 ).

0

精彩评论

暂无评论...
验证码 换一张
取 消