: This article describes how to set up the 404 page for PHP projects in a unified manner (including the yii Framework). If you are interested in the PHP Tutorial, refer to it. 1. create a custom 404 page using Apache + PHP.
First, you can use Apache's. htaccess to define a file that does not exist,
The method is to create a new. htaccess, and add ErrorDocument 404/404 .php (/404. php is a custom 404 page) at the beginning of. htaccess ).
2. set it in yii Framework
If the requested page does not exist, yii will throw a CHttpException exception with the exception code 404. how can I handle this exception in yii? there are three methods:
1. you don't need to do anything. yii will handle it by itself.
When this type of exception is thrown, yii renders the errorxxx. php (error404.php) template file under framework/view/by default.
2. create errorxxx. php under protected/views/system, and yii will render the file.
3. configure an exception processor
Add the following configuration in the configuration file main. php and set the Exception Handling Controller to site/error.
'errorHandler'=>array( // use 'site/error' action to display errors 'errorAction'=>'site/error', ),
Then add the error controller in SiteController. php:
public function actionError() { if($error=Yii::app()->errorHandler->error) {print_r($error); if(Yii::app()->request->isAjaxRequest) echo $error['message']; else $this->render('error', $error); } }
Finally, add the error. php template file under view/site:
pageTitle=Yii::app()->name . ' - Error'; $this->breadcrumbs=array( 'Error', ); ?> Error
The above introduces the PHP project unified settings 404 page (including the yii Framework), including the content, hope to be helpful to friends interested in PHP tutorials.