Home >Backend Development >PHP Tutorial >PHP projects uniformly set up 404 pages (including under the yii framework)

PHP projects uniformly set up 404 pages (including under the yii framework)

WBOY
WBOYOriginal
2016-08-08 09:31:321117browse
1. How to create a custom 404 page with Apache + PHP.

First deal with the situation where the file really does not exist. The method is to use Apache’s .htaccess definition. The

method is to create a new .htaccess and add: ErrorDocument 404 /404.php (/404. php is a custom 404 page).

2. Set up under the yii framework

When the requested page does not exist, yii will throw a CHttpException exception with the exception code 404. So how does yii handle this type of exception? There are three following Method:
1. Don’t do anything, yii will handle it by itself
When this type of exception is thrown, yii will render the errorxxx.php (error404.php) template file under framework/view/ by default
2. In protected Create a new errorxxx.php under /views/system, Yii will render the file
3. Configure the exception handler
Add the following configuration in the configuration file main.php, set the exception handling controller to site/error

'errorHandler'=>array(  
            // use 'site/error' action to display errors  
            'errorAction'=>'site/error',  
        ),  

Then in Add in SiteController.php, error controller:
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 error.php template file under view/site/:
<?php  
$this->pageTitle=Yii::app()->name . ' - Error';  
$this->breadcrumbs=array(  
    'Error',  
);  
?>  
  
<h2>Error <?php echo $code; ?></h2>  
  
<div class="error">  
<?php echo CHtml::encode($message); ?>  
</div>  

The above introduces the unified setting of 404 pages for PHP projects (including under the Yii framework), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn