前言:
而今同学们对“laravel图片处理”可能比较关注,各位老铁们都想要分析一些“laravel图片处理”的相关知识。那么小编同时在网摘上汇集了一些有关“laravel图片处理””的相关文章,希望咱们能喜欢,小伙伴们快快来了解一下吧!版本:8.0
使用
Laravel异常的处理通常通过app/Exceptions/Handler.php处理,如下
class Handler extends ExceptionHandler{ /** * Register the exception handling callbacks for the application. * * @return void */ public function register() { $this->reportable(function (Throwable $e) { }); } public function report(Throwable $exception) { parent::report($exception); // TODO: Change the autogenerated stub } public function render($request, Throwable $e) { return response()->json(Http::fail((int)$code, $message)); }}
类:app/Exceptions/Handler.php
说明
这里有两个重要方法render、report
render用于渲染错误,通过console打印、或者http response返回给客户端
report用于记录错误,通过日志等方式
如果你需要记录错误信息到日志中,就可以在report中加入自己的log逻辑如我们是这样做的
public function report(Throwable $exception){ $extraInfo = []; if ($exception instanceof HttpException) { $extraInfo['extra_info'] = [ 'status_code' => $exception->getStatusCode(), 'headers' => $exception->getHeaders(), ]; } // 记录日志 UniversalLog::getLogger(config('logging.universal.error_channel'))->warning( 'exception report : '. $exception->getMessage(), array_merge($extraInfo, ErrorContext::buildException($exception)) ); parent::report($exception); // TODO: Change the autogenerated stub}
render主要用于获取错误并将错误信息返回给客户端,或者加入自己的处理逻辑即可,如
public function render($request, Throwable $e){ $message = $e->getMessage() ?: ApiCode::CODE_MESSAGES_MAPPERS[$code] ?? ApiMessage::BUSINESS_ERROR; return response()->json(Http::fail((int)$code, $message));}实现的逻辑
那么错误是如何被Handler.php接收的呢,当程序报错了,或者异常退出了,如何捕获这个错误呢,自然有函数:set_exception_handler、set_error_handler、register_shutdown_function
那么Laravel在启动的时候会通过
类:src/Illuminate/Foundation/Bootstrap/HandleExceptions.php 进行处理,如:
class HandleExceptions{ /** * Reserved memory so that errors can be displayed properly on memory exhaustion. * * @var string */ public static $reservedMemory; /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { self::$reservedMemory = str_repeat('x', 10240); $this->app = $app; error_reporting(-1); set_error_handler([$this, 'handleError']); set_exception_handler([$this, 'handleException']); register_shutdown_function([$this, 'handleShutdown']); if (! $app->environment('testing')) { ini_set('display_errors', 'Off'); } } /** * Handle an uncaught exception from the application. * * Note: Most exceptions can be handled via the try / catch block in * the HTTP and Console kernels. But, fatal error exceptions must * be handled differently since they are not normal exceptions. * * @param \Throwable $e * @return void */ public function handleException(Throwable $e) { try { self::$reservedMemory = null; $this->getExceptionHandler()->report($e); } catch (Exception $e) { // } if ($this->app->runningInConsole()) { $this->renderForConsole($e); } else { $this->renderHttpResponse($e); } } /** * Get an instance of the exception handler. * * @return \Illuminate\Contracts\Debug\ExceptionHandler */ protected function getExceptionHandler() { return $this->app->make(ExceptionHandler::class); }}
这里呢bootStrap会在框架启动的时候调用,自然会注册异常处理函数
handlerException则是核心逻辑,这里会分别调用Handler的report及render函数。
而app/Exceptions/Handler则是src/Illuminate/Foundation/Exceptions/Handler.php的子集。
总结
重要类:
src/Illuminate/Foundation/Bootstrap/HandleExceptions.php 启动注册异常处理
src/Illuminate/Contracts/Debug/ExceptionHandler.php
src/Illuminate/Foundation/Exceptions/Handler.php 异常处理父类
app/Exceptions/Handler.php 我们自己的处理逻辑
标签: #laravel图片处理