3

I have wrote a custom 404 Page Not Found page. But where I have to put it in my Zend Application structure and how to show it?

When a controller couldn't be found I receive an Invalid controller specified (dsa) error. And when an action couldn't be found I receive an Action "wqe" does not exist and was not trapped in __call() error.

Shortly, I don't want these errors.

How can I detect if the page is not found before rendering? And when I detect it, how can I show my custom error page. At the moment I receive the errors from above but the layout is still displayed.

Do I need some .htaccess rules? Or it can be done with some Zend stuff.

1

1 Answer 1

8

In your ErrorController

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;
  }

}
2
  • just to confirm, Is above @KarmicDice code recommended/default way by Zend Framework? because for me content from $this->renderScript('error/error_404.phtml'); page is not loading unless I placed a die statement below the page.
    – FR STAR
    Feb 18, 2014 at 7:53
  • I was a 'rockstar ZF 1x' guy... It's been around 1 year since I have touched it (after ZF2s launch). Am not sure... However, if it works only with a die script, it prolly means that you are rendering other page because when u perform $this->renderScript() it sends the header information already. Did u notice I have a break; ? and this function doesn't do anything else either. Feb 19, 2014 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.