Yii2 与 Laravel
使用yii2框架的程序,默认如果访问网址不存在,将记录到错误日志。Laravel默认是不记录。让yii2实现类似Laravel的功能解决方法。
# 修改配置 config/web.php,加入自定义的捕获
'errorHandler' => [
'class' => \app\handler\ErrorHandler::class,
'errorAction' => 'site/error',
],
# 捕获文件代码
<?php
/**
* Date: 2018/5/30 23:29
* File: ErrorHandler.php
* Author: Z.B <[email protected]>
*/
namespace app\handler;
use yii\base\InvalidRouteException;
use yii\web\NotFoundHttpException;
class ErrorHandler extends \yii\web\ErrorHandler
{
public function logException($exception)
{
if ($exception instanceof NotFoundHttpException
&& $exception->getPrevious() instanceof InvalidRouteException
) {
return;
}
parent::logException($exception);
}
}
如果想实让Laravel访问404的时候也记录日志,将配置文件app/Exceptions/Handler.php
中的的这一行\Symfony\Component\HttpKernel\Exception\HttpException::class
去掉。