Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send to custom error pages Zend Framework?

I have members that can be blocked and when the member is blocked I want to send them to a custom error page, how would I do that in zend framework? I tried

throw new Zend_Controller_Dispatcher_Exception('Your message here');

but it doesn't say "your message here", it says "page not found" when I do this.

here is my error controller.

<?php

class ErrorController extends Zend_Controller_Action
{

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;

            // 666 error -- controller or action not found
            $this->getResponse()->setHttpResponseCode(666);
            $this->view->message = 'Blocked';
            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;
}

}

Here is the documentation, http://framework.zend.com/manual/en/zend.exception.using.html I don't understand it :(

like image 886
Darius Avatar asked Jun 28 '12 22:06

Darius


2 Answers

Here are a few suggestions on making it work.

First you need to differentiate your error from the standard no_route/no_controller/no_action errors. To do this, you can throw your own custom exception (you can extend this from PHP's Exception class.

throw new My_Exception_Blocked('You are blocked');

Then, modify the error controller to look like this:

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;

        // check for any other exception
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
            if ($errors->exception instanceof My_Exception_Blocked) {
                $this->getResponse()->setHttpResponseCode(403);
                $this->view->message = $errors->exception->getMessage();
                break;
            }
            // fall through if not of type My_Exception_Blocked

        default:
            // application error
            $this->getResponse()->setHttpResponseCode(500);
            $this->view->message = 'Application error';
            break;
    }
}

I also changed the response code from 666 to 403 as there are no 6xx HTTP error codes and this could upset the server and/or client.

Hope that helps.

like image 176
drew010 Avatar answered Nov 11 '22 14:11

drew010


You should not use error controller plugin to manage programatic errors. I would suggest just redirect user to a controller where they'll see required message. Error controller implementation in Zend Framework is unnecessarily complicated. You can solve the problem easily by redirecting.

Using error controller for programatic errors is also discouraged. Details from documentation is bellow.

Zend_Controller_Plugin_ErrorHandler provides a drop-in plugin for handling exceptions thrown by your application, including those resulting from missing controllers or actions; it is an alternative to the methods listed in the MVC Exceptions section.

The primary targets of the plugin are:

  • Intercept exceptions raised when no route matched

  • Intercept exceptions raised due to missing controllers or action methods

  • Intercept exceptions raised within action controllers

In other words, the ErrorHandler plugin is designed to handle HTTP 404-type errors (page missing) and 500-type errors (internal error). It is not intended to catch exceptions raised in other plugins.

like image 20
Cemal Eker Avatar answered Nov 11 '22 13:11

Cemal Eker