8

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?

davethegr8
  • 11,323
  • 5
  • 36
  • 61

4 Answers4

25

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.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • I'm trying to figure out how to have a single 404 page rather than a pages/404, and many different error pages for missing models, controllers, actions, views, etc... – davethegr8 Nov 17 '09 at 04:29
  • 6
    Create your custom 404 page in `app/views/errors/error404.ctp` and you're done. As said above, in debug mode you'll receive more detailed errors, while in production mode every 'Missing X' error will be displayed as 404 error using `error404.ctp`. – deceze Nov 17 '09 at 04:53
  • 1
    Ah, I see. I didn't realize that error404.ctp would handle *all* errors. – davethegr8 Nov 17 '09 at 16:54
  • Cool! If I could handle 503 error like this way, please? – tech_me Jun 23 '14 at 02:26
10

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');
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
Sandip Layek
  • 101
  • 1
  • 5
0

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:)

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
Michael Mao
  • 9,878
  • 23
  • 75
  • 91
0

Please add a class PostController.php not post_controller.php in the app/controller folder. It just mean that You have to create as like its class name.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Ishan
  • 1