views:

331

answers:

2

My application handles errors happily in development, both errors and exceptions. When I switch to production the application just returns a blank page as the config is set to not display errors. Is there a standard method in ZF for sending visitors to a nicely formatted 'Page not found' using a layout from the application so that they aren't presented with a blank page. Thanks in advance.

+1  A: 

You can do this with a custom ErrorController, an example:

http://www.talkincode.com/a-useful-error-controller-class-for-zend-framework-applications-729.html

Thanks I'll have a look and let you know.
ed103
Nice class, but I still seem to have the same issue, if I call hit any url that isn't specified in my acl list like myurl/foo, I get:Fatal error: Uncaught exception 'Zend_Acl_Exception' with message 'Resource 'foo' not found' in /Users/marchampson/Sites/heavenlycakes/trunk/library/Zend/Acl.php:347 Stack trace: #0 ... and a bit more, but the comment box ran out of characters...
ed103
Fixed but will leave this open for 1 more day in the hope that someone can shed some light. It looks as though the controllers were working, but... I had to put an exit; command at the end of my error.phtml view file. If it didn't stop there, I got the above fatal error. Any ideas?
ed103
+1  A: 

Usually if you used the CLI to create the zend project it will already be able to do what you request. If you go the scripts/view/errors/error.phtml that will be the phtml file you can use to create the view you are looking to use.

Although if you did not use the CLI to create the zend project bellow is the generated ErrorController used

<?php

class ErrorController extends Nanaly_Controller
{

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

        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            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;
        }

        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->crit($this->view->message, $errors->exception);
        }

        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }

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

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasPluginResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }


}

Note: this Controller was generated using Zend Version 1.10.0

and the view should be placed in the same location as stated earlier.

AlbertRosa
Thanks. I did use the CLI and it all did work, I edited the error.phtml file to add html styling so that it was in the same look and feel as the rest of the site. To get this to work in production I had to exit; at the end of error.phtml.Cheers,Marc
ed103