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后,这就不重要了。

posted @ 2015-12-16 18:40  祥子爱游戏  阅读(240)  评论(0编辑  收藏  举报