If someone hits in the browser url bar with this http://mysite/users/unknownaction then i get error.
I want to catch that error and redirect to http://mysite/ .
How can i do this in CakePHP-2.0.
Create a file "app_error.php" on "app" folder.
<?php
class AppError extends ErrorHandler {
function error404($params, $messages) {
$this->Session->flash($messages);
$this->controller->redirect(array('controller'=>'welcome', 'action'=>'index'));
parent::error404($params);
}
}
?>
You can save error message on session flash before redirect.
When you are in production mode as Hugo suggests, missing URLs will throw a NotFoundException
which, by default, generates a standard "404 Not found" page. If you wish to change this behavior, you will have to look at the documentation for handling exceptions in CakePHP 2.0 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.