How to use custom 404 page on not found controller/action
10,459
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;
}
}
Author by
enenen
Updated on June 04, 2022Comments
-
enenen almost 2 years
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 anAction "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. -
FR STAR about 10 yearsjust 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.
-
Keval Domadia about 10 yearsI 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.