phpzend-frameworkrouteszend-routezend-router

ZEND - Conflicting route patterns


I'm using Zend framework 1.12, trying to come up with custom routes.

I'm trying to create something that looks like facebook's profile URL (http://facebook.com/username). So, at first I tried something like that:

$router->addRoute(
            'eventName',
            new Zend_Controller_Router_Route(
                '/:eventName', 
                array(
                    'module' => 'default', 
                    'controller' => 'event', 
                    'action' => 'detail'
                )
            )
        );

I kept getting the following error anytime I tried running mydomain.com/something:

Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'eventName is not specified' in /var/desenvolvimento/padroes/zf/ZendFramework-1.12.0/library/Zend/Controller/Plugin/Broker.php on line 336

Not only I was unable to make that piece of code work, all my default routes were (obviously) overwritten. So I have, for example, stuff like "mydomain.com/admin" that should send me to the "admin" module, on the Index controller, but was now returning the same error (as it fell in the same pattern as /:eventName).

What I need to do is to create this custom route, without overwriting the default ones and actually working (dûh).

I have already checked the online docs and a lot (A LOT) of stuff on google, but I didn't find anything related to the error I'm getting or how to not overwrite the default routes. I'd appreciate anything that could point me the right direction.

Thanks.


EDIT¹: I managed to get it working, but I didn't use any routing at all. I just made a plugin with the following:

public function preDispatch(\Zend_Controller_Request_Abstract $request) {

    if (!\Zend_Controller_Front::getInstance()->getDispatcher()->isDispatchable($request)) {

        $request->setModuleName($this->_eventRouter["module"]);
        $request->setControllerName($this->_eventRouter["controller"]);
        $request->setActionName($this->_eventRouter["action"]);

    }

}

It feels like an ugly workaround, though... As Tim Fountain pointed out, my events are dynamic (I load them from a database), so I can't hardcode it. Also, my current implementation prevents me from having to hardcode every module/controller/action combination.

I'd just like to know if there's a way to avoid using a plugin.


EDIT²: I'm not doing that crappy plugin thing anymore. I figured out what was causing the router error. My routing definition did not have a valid default value for variable 'eventName'. My fix was:

$router->addRoute(
        'eventName',
        new Zend_Controller_Router_Route(
            '/:eventName', 
            array(
                'module' => 'default', 
                'controller' => 'event', 
                'action' => 'detail',
                'eventName' => ''
            )
        )
    );

I am still unable to create routes with "conflicting" patterns, such as /admin and /:eventName. If only there was a way to make /admin override /:eventName...


Solution

  • Routes are applied/matched on a LIFO basis. As the routing docs note:

    Note: Reverse Matching Routes are matched in reverse order so make sure your most generic routes are defined first.

    So, in order to have your "static" routes (static, in the sense that they do not pull from the db, /admin and the like) apply over your dynamic ones (/:eventName), make sure you define the static ones later in the execution flow.

    In practical terms, this means that you cannot define your static routes during bootstrap, so you'll have to do it in a plugin with a routeStartup hook. Perhaps, two plugins: one for your dynamic routes, then another for the static ones, just make sure that the priority on the plugins is set so that the static ones are added later.