English 中文(简体)
Cakephp: How would I route all missing controller/action calls to a single, general error page?
原标题:
  • 时间:2009-11-17 01:03:34
  •  标签:
  • 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?

最佳回答

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.

问题回答

Step 1:In app_controller.php add two functions

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

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

Step2: In viewslayouts create error.ctp containing echo $content_for_layout;

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

 echo $html->image( 404-not-found-1-3.jpg );

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

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签