Is there a way or a function within a controller that tells if a 404 error was triggered? I would like everyone to redirect at the homepage instead of seeing a 404 page.
CakePHP v 2.x
Using AppController::appError()
Implementing this method is an alternative to implementing a custom exception handler. It’s primarily provided for backwards compatibility, and is not recommended for new applications. This controller method is called instead of the default exception rendering. It receives the thrown exception as its only argument. You should implement your error handling in that method:
Step 1 :: File : app/Controller/AppController.php
class AppController extends Controller {
public function appError($error) {
// custom logic goes here. Here I am redirecting to a custom page
header("Location : /pages/notfound");
}
}
Step 2 :: Create custom view. app/View/pages/notfound.ctp
Write custom message in this file.
Reference :
http://book.cakephp.org/2.0/en/development/exceptions.html#using-appcontroller-apperror