开发者

Override default route but call it when necessary

开发者 https://www.devze.com 2022-12-22 10:41 出处:网络
I\'m developing a site which has urls like this: site.com/james site.com/james/photos Schema for this urls are this: site.com/USERNAME/APPLICATION

I'm developing a site which has urls like this:

site.com/james
site.com/james/photos

Schema for this urls are this: site.com/USERNAME/APPLICATION

Both username and application names are checking from db yb program logic. I set the route for this but Zend is routing all requests to this route.

But there is same controllers and admin module. When i go to site.com/admin, it looks for username "admin". Or when i go to site.com/james/profile, it tries to find the application named "profile".开发者_高级运维 But there is a action in UserController for this.

How can i implement this feature? For example a front controller which looks for names of Controllers and dispatch the requst to them if controller exists?


Have a look at overriding the Zend_Controller_Action __call($method, $args) method which is invoked if the desired action cannot be found. You could put your site.com/USERNAME/APPLICATION checking logic in the __call method of, say, the default controller (or wherever makes most sense) and go back to using the default route.


Generally, if you do this, you'll have to reinstate the 'default' routes afterwards for them to work. This literally means adding a route that matches /admin and goes to the admin module.

This soon becomes a pain to maintain if your app is quite big. You can make things easier by changing your url schemes to something like site.com/user/james and site.com/user/james/photos.

One alternative which I use for content driven websites is to route all requests that don't match an existing controller to the default controller/action:

$front->setParam('useDefaultControllerAlways', true);

By default, unrouteable requests will map to IndexController::indexAction


I'd use route chaining. Remove default routes, and add it at the end of the chain.

Eventually a front controller plugin to handle the conditions.

0

精彩评论

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

关注公众号