laravel 5.1自定义异常

只是贴下我的代码就是好了

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler {
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
HttpException::class,
ModelNotFoundException::class,
];

/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e) {
return parent::report($e);
}

/**
* Render an exception into an HTTP response.
* @idea 2015/12/14修改
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e) {
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
} elseif ($e instanceof HttpException && 404 == $e->getStatusCode() && $this->isPic()) {
return response(\Illuminate\Support\Facades\File::get(public_path('static/images/404.png')), 404, ['Content-type' => 'image/png']);
}

return parent::render($request, $e);
}

/**
* @power 判断url是否是pic
* @return bool TRUE|FALSE
*/
private function isPic() {
if (!isset($_SERVER['REQUEST_URI'])) {
return FALSE;
}

$array = pathinfo($_SERVER['REQUEST_URI']);

if (empty($array['extension'])) {
return FALSE;
}

return preg_match('/png|jpg|gif|jpeg/', $array['extension']);
}

}
从namesapce就可以看到这个class的文件了吧,从class就可以看到这个文件的名字了吧。

ps:只是修改了laravel的内置异常。而且处理图片的404异常只在单服务器才有用,一旦用上了cdn后,这就不重要了。

转载于:https://www.cnblogs.com/zzx11235/articles/5052040.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值