rjjonker
rjjonker

Reputation: 73

How to display Custom Error message in Zend Framework?

I have a ZF application. In my bootstrap.php I defined the routes for my application. Now, when I type an URL in the address bar that does not match any of the routes, I get a fatal error. I don't want that, I just want to show a 'page can not be found' error. How come the application throws a fatal error, instead of going to my error controller?

This is what my routes look like:

$router->addRoute('page1',
        new Zend_Controller_Router_Route_Static('page1', array(
            'controller' => 'mycontroller',
            'action' => 'index',
            'id' => '2626'
        ))
    );

For example, if I browse to /page2 I get a fatal error, because it goes to 'mycontroller' anyway, and tries to do stuff I do in that controller. But I don't want it to go there.

Thanks in advance for your replies.

Upvotes: 0

Views: 1736

Answers (1)

Keval Domadia
Keval Domadia

Reputation: 4763

ErrorController should be like this.

class ErrorController extends Zend_Controller_Action {

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

    if (!$errors || !$errors instanceof ArrayObject) {
      $this->view->message = 'You have reached the error page';
      return;
    }

    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);
        $priority = Zend_Log::NOTICE;
        $this->view->error_code = $this->getResponse()->getHttpResponseCode();
        $this->view->message = "Page Not Found";
        $this->renderScript('error/error_404.phtml');
        break;
      default:
        // application error
        print_r($this->getResponse());
        $this->getResponse()->setHttpResponseCode(500);
        $priority = Zend_Log::CRIT;
        $this->view->error_code = $this->getResponse()->getHttpResponseCode();
        $this->view->message = 'Application error';
        if ($log = $this->getLog()) {
        $log->log($this->view->message, $priority, $errors->exception);
        $log->log('Request Parameters', $priority, $errors->request->getParams());
        $this->renderScript('error/error_500.phtml');
        }

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

        $this->view->request = $errors->request;
        $this->view->error_code = $this->getResponse()->getHttpResponseCode();
        $this->renderScript('error/error_500.phtml');
        break;
    }

    // Log exception, if logger available
    if ($log = $this->getLog()) {
      $log->log($this->view->message, $priority, $errors->exception);
      $log->log('Request Parameters', $priority, $errors->request->getParams());
    }

    // 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->hasResource('Log')) {
      return false;
    }
    $log = $bootstrap->getResource('Log');
    return $log;
  }

}

Phew! You can define any HTTP Status Response Code using $this->getResponse()->HttpResponseCode()

Also, we per best practices (in quotes), check that LOGS are required. !

Questions? :)

Upvotes: 1

Related Questions