Yii 错误处理
Yii包含一个内置的错误处理程序。Yii错误处理程序执行以下操作 -
- 将所有非致命PHP错误转换为可捕获的异常。
- 用详细的调用堆栈显示所有错误和异常。
- 支持不同的错误格式。
- 支持使用控制器操作来显示错误。
要禁用错误处理程序,应该在入口脚本中将YII_ENABLE_ERROR_HANDLER常量定义为false。错误处理程序被注册为应用程序组件。
第1步 - 您可以通过以下方式进行配置。
return [
'components' => [
'errorHandler' => [
'maxSourceLines' => 10,
],
],
];
以上配置将要显示的源代码行数设置为10.错误处理程序将所有非致命PHP错误转换为可捕获的异常。
第2步 - 向SiteController 添加一个名为 actionShowError() 的新函数。
public function actionShowError() {
try {
5/0;
} catch (ErrorException $e) {
Yii::warning("Ooops...division by zero.");
}
// execution continues...
}
第3步 - 转到URL http:// localhost:8080 / index.php?r = site / show-error 。您将看到一条警告消息。

如果你想向用户显示他的请求无效,你可以抛出 yii \ web \ NotFoundHttpException 。
第4步 - 修改 actionShowError() 函数。
public function actionShowError() {
throw new NotFoundHttpException("Something unexpected happened");
}
第5步 - 在地址栏中输入地址 http:// localhost:8080 / index.php?r = site / show- error 。您将看到以下HTTP错误。

当YII_DEBUG常量为真时,错误处理程序将显示具有详细调用堆栈的错误。常数为假时,只会显示错误消息。默认情况下,错误处理程序使用这些视图显示错误 -
- @ yii / views / errorHandler / exception.php - 使用调用堆栈信息显示错误时使用视图文件。
- @ yii / views / errorHandler / error.php - 当没有调用堆栈信息时应该显示错误时使用视图文件。
您可以使用专用的错误操作来自定义错误显示。
第6步 - 修改 config / web.php 文件中的 errorHandler 应用程序组件。 **
<? php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this
//is required by cookie validation
'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
**'errorHandler' => [
'errorAction' => 'site/error',
],**
//other components...
'db' => require(__DIR__ . '/db.php'),
],
'modules' => [
'hello' => [
'class' => 'app\modules\hello\Hello',
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
?>
上面的配置定义,如果在没有调用堆栈的情况下需要显示 错误, 则会执行 站点/错误 操作。
第7步 - 修改 SiteController 的 actions() 方法。
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
上面的代码定义,当发生 错误 时,错误视图将被渲染。
第8步 - 在views / site目录下创建一个名为 error.php 的文件。
<?php
/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */
use yii\helpers\Html;
$this->title = $name;
?>
<div class = "site-error">
<h2>customized error</h2>
<h1><?= Html::encode($this->title) ?></h1>
<div class = "alert alert-danger">
<?= nl2br(Html::encode($message)) ?>
</div>
<p>
The above error occurred while the Web server was processing your request.
</p>
<p>
Please contact us if you think this is a server error. Thank you.
</p>
</div>
第9步 - 转到地址 http:// localhost:8080 / index.php?r = site / show-error ,您将看到自定义的错误视图。

下一章:Yii 认证
验证用户身份的过程称为 验证 。它通常使用用户名和密码来判断用户是否是他自称的用户。要使用Yii身份验证框架,您需要 -配置用户应用程序组件。实现yii \ web \ IdentityInterface接口。基本的应用程序模 ...
AI 中文社