CakePHP: Redirecting all 404 errors to the homepage?

后端 未结 4 1829
遥遥无期
遥遥无期 2021-01-07 11:17

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.

4条回答
  •  一向
    一向 (楼主)
    2021-01-07 11:47

    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

提交回复
热议问题