English 中文(简体)
Zend Framework: How to disable default routing?
原标题:

I ve spent many hours trying to get this to work. And I m getting quite desperate. Would be great if someone out there could help me out :)

Currently using Zend Framework 1.9.5, though I have been struggling to get this to work for many versions now.

What I want to do is provide my own routes through an XML config, and make sure that everything that is not defined in my config will end up on my errorController. (preferably in a way so I can em apart from EXCEPTION_NO_CONTROLLER and EXCEPTION_NO_ACTION)

I figured that this means I have to get rid of default /:module/:controller/:action and /:controller/:action routes.

So when I tell the router to removeDefaultRoutes(), it won t match these default routes anymore. But now the router is now routing every unrouted route to the defaultcontroller::defaultaction (What the ??)

$front->getRouter()->removeDefaultRoutes();

So, anyone know how to make the frontcontroller (or a part of it) throw an exception when an URI can not be routed?

Reason I want to do this is to prevent duplicate content, and have better 404 pages (in this case, no controller / no action errors are actually application errors instead of not-found)

最佳回答

did you try adding a new route like

$route = new Zend_Controller_Router_Route( * , array( controller => error ,  module => default ,  action => error ));


$router->addRoute( default , $route);

You need to add this route first as it needs to be the last processed.

问题回答

Fast forward in time to one year later... (time travel music)

Here s another way that I think is much less "intrusive". You can write a plugin to catch the default route and when that happens just throw an exception which at the end of the whole cycle gets translated into a 404 by the front controller.

class Application_Plugin_DisableDefaultRoutes extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $front = Zend_Controller_Front::getInstance();
        $currentRoute = $front->getRouter()->getCurrentRouteName();
        if ($currentRoute ==  default ) {
            throw new Exception( Default route is disabled );
        }
    }
}

You can load your plugin in Bootstrap.php

protected function _initPlugins()
{
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new Application_Plugin_DisableDefaultRoutes());
}

With this way you can load the plugin in the production machine and leave it out in development where you might want to use the default route for quick tests or something else.





相关问题
Rails: Proxy Pass?

I ve got a web-app that I want to migrate to Rails, which is currently just plain HTML with an Apache proxy to another server, running a custom database/webserver that serves the site s dynamic ...

MVC routing is not handling one of my directories

I m using ASP.NET MVC with IIS 7.0. I ve got 404 errors hooked up fine through my Application_Error override. In addition to "Controllers", "Models", "Helpers" etc. I have a directory called Files ...

Ruby on Rails conditional routing

I have a site listing many jobs, but I also want each account to be able to access its jobs in one place. Thus, I am using these routes: map.resources :jobs map.resource :account, :has_many => :...

Clean URL s using OpenCart s router class

How do you write clean URL s in OpenCart using their built in Router class? Here is my .htaccess file: RewriteEngine On RewriteRule ^(system) - [F,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{...

subdomain routing to multiple controller issue

I have a site: www.mydomain.com where we have administrative controls hidden away from normal customer view. I would like to only access the administrative features under a subdomain such as admin....

Can controller names in RESTful routes be optional?

With a standard map.resource routing mechanics and several nested resources the resultant routes are unnecessarily long. Consider the following route: site.org/users/pavelshved/blogs/blogging-horror/...

Help with rails routes

Im having a little trouble setting up routes. I have a users controller/model/views set up restfully so users is set up to be a resource in my routes. I want to change that to be usuarios ...

热门标签