ZF: Managing 404 Errors with Version 1.0.3

Ok, the description of the Link referring to the version 0.9 solution for Managing 404 errors due to missing modules or actions is wrong. It is not enough to replace Zend::loadClass with Zend_Loader::loadClass. Other functions have been renamed, my current snippet is:

 * Original Snippet from: http://www.bigroom.co.uk/blog/managing-404-errors-in-the-zend-framework
    class NoroutePlugin extends Zend_Controller_Plugin_Abstract
        public function preDispatch(Zend_Controller_Request_Abstract $request )
            $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();

            $controllerName = $request->getControllerName();
            if (empty($controllerName)) {
                $controllerName = $dispatcher->getDefaultControllerClass($request);
            $className = $dispatcher->formatControllerName($controllerName);
            if ($className)
                    // if this fails, an exception will be thrown and
                    // caught below, indicating that the class can't
                    // be loaded.
                    Zend_Loader::loadClass($className, $dispatcher->getControllerDirectory());
                    $actionName = $request->getActionName();
                    if (empty($actionName)) {
                        $actionName = $dispatcher->getDefaultAction();
                    $methodName = $dispatcher->formatActionName($actionName);

                    $class = new ReflectionClass( $className );
                    if( $class->hasMethod( $methodName ) )
                        // all is well - exit now
                catch (Zend_Exception $e)
                    // Couldn't load the class. No need to act yet,
                    // just catch the exception and fall out of the
                    // if

            // we only arrive here if can't find controller or action
            $request->setControllerName( 'blog' );
            $request->setActionName( 'noroute' );
            $request->setDispatched( false );

I hope someone needs this as much as me.

More about: ZendFramework

Sign up for my newsletter to get notified when I post new content on this blog and with the occasional exclusive content only for subscribers.

By clicking on the Subscribe button I am giving my consent for Benjamin Eberlei to hold my name and email address for the purposes of contacting me with a newsletter on the topics of this blog. You can unsubscribe with one click at any time and withdraw your consent. No spam. I will never share your e-mail address. Privacy Policy