Laravel 框架中 使用 Symfony 提供的方法 丟擲不同型別錯誤異常

LW_aravel發表於2018-10-17

Symfony\Component\HttpKernel\Exception\BadRequestHttpException 400
Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException 401
Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException 403
Symfony\Component\HttpKernel\Exception\NotFoundHttpException 404
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 405
Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException 406
Symfony\Component\HttpKernel\Exception\ConflictHttpException 409
Symfony\Component\HttpKernel\Exception\GoneHttpException 410
Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException 411
Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException 412
Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException 415
Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException 428
Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException 429
Symfony\Component\HttpKernel\Exception\HttpException 500
Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException 503

在需要丟擲對應型別的錯誤的時候,只需要引入上面對應的 異常處理類,呼叫對應 方法就可以
例如 丟擲一個404錯誤
throw new BadRequestHttpException('請求的資源不存在!');

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章