Question

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

Was it helpful?

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.

OTHER TIPS

The error you are getting is probably coming from a URL helper call you have in your template. You need to specify the eventName param to this since you've made it required, e.g.:

<a href="<?=$this->url(array('eventName' => 'foo'), 'eventName')?>">Something</a>

The answer to your other question depends a bit on whether you have a static, unchanging list of events or non-event URLs. You need to give the router a way to determine whether /foo is an event, or a controller. You do this by either hardcoding the possible events in to your event route, hardcoding routes for your other non-event URLs, or (if your events are dynamic and based on some database content) writing a custom route class for your event route which can do a lookup to see whether a given string is an event.

Since you are using Zend Framework 1.x

Here is the solution which I have added here : How to redirect Error Page and perform Routes in Zend Framework 1.x

Also, to make life easier... here it is:

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

Once you are on the action which calls your eventName, you can put a check if that == admin, later you can define a re-route by specifying which action needs to be loaded, in that condition itself.

Simple? :)

define the eventName, and even if it's not required, just leave it blank.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top