Cakephp: How would I route all missing controller/action calls to a single, general error page?

前端 未结 4 1950
夕颜
夕颜 2020-12-16 22:02

I\'ve got a cakephp app that I\'m trying to get to serve up the Pages::404 function (and corresponding view) whenever Cake encounters any error (missing control

4条回答
  •  甜味超标
    2020-12-16 22:17

    Are you in the controller when you are trying to redirect to the 404 error page?

    Well if that is the case, you can walk around the problem like this:

    Copy and paste the error layout (error404.ctp) from the cake core library directory into yours app/views/errors/

    Then add the following line whenever you encounter an error inside a controller.

    $this->cakeError('error404',array(array('url'=>'/'))); 
    

    Oh, another way to handle this is to edit the routes.php file in app/config

    CakePHP Official site Routes-Configuration

    I don't have a working copy of CakePHP at the moment, so I would just describe the basic logic here(what you can do inside the routes.php file)

    Redirect traffic with specific url patterns(say, http://yourwebsite/validController/validFunction/validParam) to their corresponding destinations respectively. Redirect all other traffic (missing controller, model, view, etc) to 404 page.

    Hope that helps:)

提交回复
热议问题