Home >PHP Framework >YII >yii2 404 error handling

yii2 404 error handling

藏色散人
藏色散人Original
2020-01-09 10:28:073642browse

yii2 404 error handling

yii2 404 error handling

The frontend and backend configuration methods are the same. The frontend is demonstrated here

1. First go to the frontend\config folder of the yii framework, there is a main.php file in it, open it for editing, and modify the controller name and method name:

'errorHandler' => [
'errorAction' => 'common/error',
],

Recommended learning: yii framework

As shown in the picture:

yii2 404 error handling

2. Then go to controllers to create your controller and write the following code:

/**
 * 404友好页面
 */
public function actions(){
return [
'error' => [
        'class' => 'yii\web\ErrorAction',
        ],
        'captcha' => [
        'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
        ,
];
}

As shown:

yii2 404 error handling

3. Finally create your view on the V layer (here is error.php):

<?php
    $this->context->layout = false; //不加载公共样式
?>
<!DOCTYPE html>
<html>
<head>
<title>404错误,您所访问的页面不存在!</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css"/>
<link rel="stylesheet" href="css/base.css"/>
</head>
<body>
<div id="errorpage">
    <div class="tfans_error">
        <div class="logo"></div>
        <div class="errortans clearfix">
            <div class="e404"></div>
            <p><b>出错啦!</b></p>
            <p>您访问的页面不存在</p>
            <div class="bt" ><a href="?r=index/index">返回首页</a></div>
        </div>
    </div>
</div>
</body>
</html>

As shown :

yii2 404 error handling##

The above is the detailed content of yii2 404 error handling. For more information, please follow other related articles on the PHP Chinese website!

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