Jump to content

[SOLVED] Zend FW 1.5 ErrorController Issues


blinky001

Recommended Posts

Wondering if anyone can help me with the Zend Framework (1.5 using MVC). I'm pretty new to it and am currently having no luck with the error controller.

 

Basically I have a very minimal test setup (just to teach myself the thing) and I have 3 'modules' defined - public (default), shared, and admin. Public uses the preDispatch method of the Zend_Controller_Plugin_Abstract to send all public traffic to the indexController / indexAction:

 

<?php

# Route all requests to the index controller / index action
class APP_Controller_Plugin_CleanUrls extends Zend_Controller_Plugin_Abstract
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        if($request->module == 'public')
        {
            # This is the dynamic part of the site - route everything to the index controller / index action
            $request->setControllerName('index');
            $request->setActionName('index');
        }
        else
        {
            echo 'using: '.$request->module.' module<br />';
        }
    }

}

 

and for now lets me know which module I am using if it is not the default (public) one. The public index controller works as planned handling 404s / 500s etc itself without the error controller. However, I would want this enabled for the admin part of a site. The important part of my bootstrap file is as follows:

 

<?php

# Get the front controller
$frontController = Zend_Controller_Front::getInstance();

$frontController->setControllerDirectory(array(
    'public'    => APP_DIR.'core'.DS.'public'.DS.'controllers',
    'shared'    => APP_DIR.'core'.DS.'shared'.DS.'controllers', 
    'admin'     => APP_DIR.'core'.DS.'admin'.DS.'controllers'
));

$frontController->setDefaultModule('public');

$frontController->registerPlugin(new APP_Controller_Plugin_CleanUrls());

 

I have fixed the httpd.conf file to remove the default handling of '/error' by apache by commenting the 'Include conf/extra/httpd-multilang-errordoc.conf' line out. My admin IndexController file works fine, as does the admin ErrorController file if I reference it absolutely: http://local.zendTest/admin/error/error. However, if I try http://local.zendTest/admin/error/errorX or http://local.zendTest/admin/X I get the following:

 

using: admin module
using: admin module
using: admin module

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' 
in C:\Server\htdocs\cms\framework\Zend\Controller\Dispatcher\Standard.php:249 

Stack trace: 
#0 C:\Server\htdocs\cms\framework\Zend\Controller\Front.php(914): 
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) 
#1 C:\Server\htdocs\cms\sites\site1\public\index.php(9): 
Zend_Controller_Front->dispatch() 
#2 {main} thrown in C:\Server\htdocs\cms\framework\Zend\Controller\Dispatcher\Standard.php on line 249

 

Not sure what I am doing wrong? My admin ErrorController looks like this (pretty much straight out of the manual) where APP_Controller_Action is my own override:

 

<?php 

class Admin_ErrorController extends APP_Controller_Action
{
    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');

        if(is_object($errors))
        {
            switch($errors->type)
            {
                case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
                case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
     
                    # 404 error -- controller or action not found 
                    $this->getResponse()->setHttpResponseCode(404);
                    $this->view->message = 'Page not found';
                    break;
                default:
                    # application error
                    $this->getResponse()->setHttpResponseCode(500);
                    $this->view->message = 'Application error';
                    break;
            }
     
            $this->view->title      = 'Errors';
            $this->view->env        = $this->getInvokeArg('env');
            $this->view->exception  = $errors->exception;
            $this->view->request    = $errors->request;
        }
        else
        {
            $this->view->title      = 'No Errors';
            $this->view->message    = 'No Errors Detected';
        }
    }
}

 

Can anyone shed any light on this situation at all?

 

Thanks in advance,

 

Paul

Link to post
Share on other sites

Found a work around online (credit to Daniel Cousineau). Apparently the ZF does not allow for multiple error controllers by default - God knows why not - it is very useful to be able to handle errors differently for different modules...

 

<?php

class APP_Controller_Plugin_ErrorControllerSelector extends Zend_Controller_Plugin_Abstract
{

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $front = Zend_Controller_Front::getInstance();

        # If the ErrorHandler plugin is not registered, bail out
        if(!($front->getPlugin('Zend_Controller_Plugin_ErrorHandler') instanceOf Zend_Controller_Plugin_ErrorHandler))
        {
            return;
        }

        $error = $front->getPlugin('Zend_Controller_Plugin_ErrorHandler');

        # Generate a test request to use to determine if the error controller in our module exists
        $testRequest = new Zend_Controller_Request_HTTP();
        $testRequest->setModuleName($request->getModuleName())
                    ->setControllerName($error->getErrorHandlerController())
                    ->setActionName($error->getErrorHandlerAction());

        # Does the controller even exist?
        if($front->getDispatcher()->isDispatchable($testRequest))
        {
            $error->setErrorHandlerModule($request->getModuleName());
        }
    }

} 

 

and then in the bootstrap just register an extra plugin:

 

<?php

$frontController->registerPlugin(new APP_Controller_Plugin_ErrorControllerSelector());

?> 

 

Hope this helps others...

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.