Jump to content

[SOLVED] Zend_Controller_Plugin_ErrorHandler within modular structure?


Recommended Posts

Given the following directory structure.

 

[pre]

app

|-- bootstrap.php

|-- conf

|-- libs

|-- modules

|  |-- admin

|  |  |-- controllers

|  |  |  `-- IndexController.php

|  |  `-- views

|  |      |-- filters

|  |      |-- helpers

|  |      `-- scripts

|  |          `-- index

|  |              `-- index.phtml

|  |-- default

|  |  |-- controllers

|  |  |  `-- IndexController.php

|  |  `-- views

|  |      |-- filters

|  |      |-- helpers

|  |      `-- scripts

|  |          `-- index

|  |              |-- foo.phtml

|  |              `-- index.phtml

|  `-- error

|      |-- controllers

|      |  `-- ErrorController.php

|      `-- views

|          |-- filters

|          |-- helpers

|          `-- scripts

|              `-- error

|                  `-- error.phtml

|-- tmp

`-- wwwroot

    |-- css

    |-- imgs

    |-- index.php

    `-- js

[/pre]

 

I'm having a hell of a time getting the Zend_Controller_Plugin_ErrorHandler plugin to load when there is a 404 type (controller not found) error.

 

bootstrap.php

<?php

defined('LIBS_PATH') or define('LIBS_PATH', APP_PATH . DS . 'libs');
defined('APP_PATH') or define('APP_PATH', dirname(__FILE__));
defined('APP_ENV') or define('APP_ENV', 'development');
defined('MODS_PATH') or define('MODS_PATH', APP_PATH . DS . 'modules');

Zend_Controller_Front::getInstance()
    ->setRequest(new My_Controller_Request)
    ->addModuleDirectory(MODS_PATH)
    ->registerPlugin(
        new Zend_Controller_Plugin_ErrorHandler(
            array(
                'module'     => 'error',
                'controller' => 'Error_ErrorController',
                'action'     => 'errorAction'
            )   
        )   
    )   
    ->setParam('env', APP_ENV)
;   

echo "<pre>";
print_r($front->getPlugins());
echo "</pre>";

 

wwwroot/index.php

<?php

define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);

define('APP_PATH', realpath(dirname(__FILE__) . '/..'));
define('LIBS_PATH', APP_PATH . DS . 'libs');
define('MODS_PATH', APP_PATH . DS . 'modules');

set_include_path(get_include_path() . LIBS_PATH . PS);

require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();

try {
    require '../bootstrap.php';
} catch (Exception $exception) {
    echo '<html><body><center>An exception occured while bootstrapping the application.';
    if (defined('APP_ENV') && APP_ENV != 'production') {
        echo '<br /><br />' . $exception->getMessage() . '<br />'
           . '<div align="left">Stack Trace:'
           . '<pre>' . $exception->getTraceAsString() . '</pre></div>';
    }
    echo '</center></body></html>';
    exit(1);
}

Zend_Controller_Front::getInstance()->dispatch();

 

modules/error/controllers/ErrorController.php

<?php

class Error_ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        // Ensure the default view suffix is used so we always return good 
        // content
        $this->_helper->viewRenderer->setViewSuffix('phtml');

        // Grab the error object from the request
        $errors = $this->_getParam('error_handler');

        // $errors will be an object set as a parameter of the request object, 
        // type is a property
        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->env       = $this->getInvokeArg('env');


        $this->view->exception = $errors->exception;

        $this->view->request   = $errors->request;
    }
}

 

produces:

[pre]

Array

(

  [0] => Zend_Controller_Plugin_ErrorHandler Object

      (

          [_errorModule:protected] => error

          [_errorController:protected] => Error_ErrorController

          [_errorAction:protected] => errorAction

          [_isInsideErrorHandlerLoop:protected] =>

          [_exceptionCountAtFirstEncounter:protected] => 0

          [_request:protected] =>

          [_response:protected] =>

      )

 

)

 

 

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception'

with message 'Invalid controller specified (Error_ErrorController)' in

C:\Inetpub\libs\Zend\Controller\Dispatcher\Standard.php:241 Stack

trace: #0 C:\Inetpub\libs\Zend\Controller\Front.php(936):

Zend_Controller_Dispatcher_Standard->dispatch(Object(My_Controller_Request),

Object(Zend_Controller_Response_Http)) #1

C:\Inetpub\wwwroot\index.php(28): Zend_Controller_Front->dispatch() #2

{main} thrown in

C:\Inetpub\libs\Zend\Controller\Dispatcher\Standard.php on line 241

[/pre]

 

Obviously the array I'm passing to Zend_Controller_Plugin_ErrorHandler doesn't define the proper perameters or something but Ive been through the docs many a time and just can't get it sorted. Anyone with more Zend experience got any thoughts?

The error message is quite simple :)

 

It is having trouble finding the location of the Error_ErrorController.

 

Question I have that will solve this very simply:

 

Can you properly load other controllers using this setup ... If you can load another controller double and tripe check that structure is correct and nothing is misspelled somewhere.

 

If you cannot load a controller using this method give me some more info on how exactly you are loading your controllers I can surely help you out with this :)

 

Also, to keep things a bit simpler you could do this and perform the same thing.

 

                'controller' => 'Error_ErrorController',
                'action'     => 'errorAction'

 

The ErrorController can be dropped from the name as Error_ Prefix will automatically be added from the module....and ErrorController will be parsed in automatically also, so you can simply supply "error" and the framework will automatically append/prepend what is needed.

 

Also for the action "errorAction" can simply become "error" as the same thing applies as above.

 

I don't see any setup for the controllers loading directories but from my experience using modules the structual setup should be something like this

 

app

|

| -- controllers

|     | -- admin (module)

|           `-- IndexController.php

|           `-- UserController.php

|     | -- error (module)

|           `-- ErrorController.php

Sorry, I should have marked this solved already.

 

I ended up moving my ErrorController.php and its related view scripts out of its own module and into default (and another in admin). This allows me to have a different error page depending on what module was called anyway, so its a little more flexable anyways.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.