Zend Framework handles exceptions correctly in production

My application successfully handles development errors, both errors and exceptions. When I switch to production, the application simply returns a blank page, as errors are not displayed in the configuration settings. Is there a standard method in ZF for sending visitors to a well-formatted “page not found” using the layout from the application so that they are not represented by a blank page. Thanks in advance.

+3
source share
2 answers

Usually, if you used the CLI to create a zend project, it will already be able to accomplish what you request. If you run the scripts /view/errors/error.phtml, it will be a phtml file that you can use to create the view you want to use.

Although, if you did not use the CLI to create the zend project below, the created ErrorController is 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 presentation should be located in the same place as previously indicated.

+2
source
+2
source

Source: https://habr.com/ru/post/1730762/


All Articles