Configure the 404 page for the PHP project in a unified manner (including the yii Framework)

Source: Internet
Author: User
Tags php template
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.