Filtering request in Zend Router

Go To StackoverFlow.com

0

I am a novice as far as writing Zend routers are concerned. I want to route all requests of the form

/:username to controller=>user and action=>profile. The catch is that I want to able to filter which :username will be routed. i.e. I want to filter the usernames will be routed.

The most basic and important filter that I want to apply, is that if the :username matches some existing controller name, it should not get routed to /user/profile.

Any help on this would be deeply appreciated. Thanks.

2012-04-05 22:23
by Vishesh Joshi


1

Here are some hints that should get you to where you want to be:

  1. Routes are aggregated onto a stack so they are LIFO (last in, first out). This means that your highest priority route should be defined last. For this particular scenario, you want your /:username route to be listed earlier so it has a lower priority. This will allow your other existing routes to get routed first.
  2. You mentioned you want to "filter" which :username will be routed. Do you have a static list of usernames? If so, you can use a Regex route and do (username1|username2|...). If you are looking up usernames via database, you'll need to build a custom route class that queries your database.
  3. Alternatively, you can simply use the pattern based route, but then do your filtering in a front controller plugin.

NOTE: If you go with option #3, the following should get you started:

$router       = Zend_Controller_Front::getInstance()->getRouter();
$matchedRoute = $router->getCurrentRouteName();

You would take action based on the route name. If it isn't the route name that defines the /:username route, then ignore. If it does match, look up the username and filter.

2012-04-06 06:26
by Wil Moore III
FYI, you also might want to check out this Zendcasts episode: http://www.zendcasts.com/creating-custom-zend_controller-routes-2/2009/01 - Wil Moore III 2012-04-06 06:27
Ads