Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

cakephp

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 controller, action, etc).

What's the best way to do that?

like image 950
davethegr8 Avatar asked Nov 17 '09 01:11

davethegr8


2 Answers

Cake automatically throws a 404 error for missing methods or controllers. While in debug mode, this error takes the form of a detailed error message containing instructions, like:

Missing Controller

Error: FooController could not be found.

Error: Create the class FooController below in file: > app/controllers/foo_controller.php

Notice: If you want to customize this error message, create app/views/errors/missing_controller.ctp

In production mode (debug = 0) the message just looks like this:

Not Found

Error: The requested address '/foo' was not found on this server.

These error pages are defined in cake/libs/view/errors/. As the message in debug mode says, you can create your own, custom error pages (using the same name as the ones in the cake/ directory) in app/views/errors/.

If you want to execute a custom function on errors, you'll best put it in the AppError Controller as described in Error Handling.

like image 50
deceze Avatar answered Oct 17 '22 01:10

deceze


Step 1:In app_controller.php add two functions

function _setErrorLayout() {  
     if ($this->name == 'CakeError') {  
        $this->layout = 'error';  
     }    
}              

function beforeRender () {  
      $this->_setErrorLayout();
    }
}

Step2: In views\layouts\ create error.ctp containing echo $content_for_layout;

step:3 In views\errors\ make missing_action.ctp and customize the page as you need my PHP code was:

 echo $html->image('404-not-found-1-3.jpg');
like image 10
Sandip Layek Avatar answered Oct 17 '22 02:10

Sandip Layek