Nơi nào tôi bắt ngoại lệ MethodNotAllowedHttpException trong Laravel 5. Trong laravel 4 tôi có thể làm điều này trong start/global.php
Laravel 5 - Làm thế nào để xử lý MethodNotAllowedHttpException
5
A
Trả lời
14
// Exceptions/Handler.php
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
public function render($request, \Exception $e)
{
if ($e instanceof MethodNotAllowedHttpException) {
// …
}
return parent::render($request, $e);
}
+0
Nếu bạn gặp sự cố khi sử dụng 'code'instanceof'code' với php 7.1, bạn có thể sử dụng' code'is_a() 'code'. Nếu ai đó có một lời giải thích tại sao tôi không nhận được 'code'true'code' với' code'instanceof'code'? Cảm ơn. –
3
Trong Laravel 5.4
, tôi đã làm điều đó như thế này:
file location: app/Exceptions/Handler.php
Thêm mã này ở phía trên cùng của tập tin:
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
Và thay đổi mã phương pháp như belows:
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if ($exception instanceof MethodNotAllowedHttpException)
{
return response()->json([
'success' => 0,
'message' => 'Method is not allowed for the requested route',
], 405);
}
return parent::render($request, $exception);
}
Các vấn đề liên quan
- 1. MethodNotAllowedHttpException laravel-4
- 2. Làm thế nào để xử lý ngoại lệ và thông báo lỗi trong Laravel 5?
- 3. Laravel 5: Xử lý nhiều kết nối và thử nghiệm
- 4. Làm thế nào để nén HTML trong laravel 5
- 5. Làm thế nào để tạo HMVC trong Laravel 5?
- 6. Làm thế nào để tắt cookie trong Laravel 5?
- 7. Quản lý tài sản trong Laravel 5
- 8. Làm thế nào để bỏ qua xử lý ngoại lệ Laravel
- 9. Làm thế nào để xử lý datetime giữa php (Laravel api) và javascript (AngularJS)
- 10. Laravel: Tuyến đường :: tài nguyên() GET & POST làm việc, nhưng PUT & DELETE ném MethodNotAllowedHttpException
- 11. MethodNotAllowedHttpException phù RouteCollection.php 219
- 12. Làm thế nào để xử lý nhiều đại biểu
- 13. PHP Làm thế nào để lưu dữ liệu từ mảng để mysql sử dụng laravel 5
- 14. Làm thế nào để xử lý AccessDeniedException trong Spring Security?
- 15. Làm cách nào để xử lý các ngày không đặt/mặc định trong Laravel? (với Carbon)
- 16. Laravel 5 Error Reporting Suppression
- 17. Làm thế nào để xử lý StackOverflowError trong Java?
- 18. Làm thế nào để xử lý 'undefined' trong javascript
- 19. Làm thế nào để xử lý đúng ThreadInterruptedException?
- 20. Làm thế nào để xử lý autorotation trong AVCaptureVideoPreviewLayer?
- 21. Làm thế nào để xử lý một đối tượng MemoryStream
- 22. Làm thế nào để xử lý deserialisation chung trong haskell?
- 23. Alamofire: Làm thế nào để xử lý 401 toàn cầu?
- 24. Làm thế nào để xử lý tải trong AngularJS?
- 25. Làm thế nào để xử lý ListView bấm trong Android
- 26. Làm thế nào để xử lý ruby chia zero?
- 27. Làm thế nào để Django Fixtures xử lý ManyToManyFields?
- 28. Làm thế nào để xử lý JSON trong Django
- 29. Làm thế nào để xử lý chính xác System.Net.Mail.SmtpException?
- 30. Làm thế nào để xử lý gián đoạn Git?
Tại sao bạn muốn bắt nó? Nếu bạn gửi yêu cầu POST trên một tuyến GET, bạn sẽ nhận được ngoại lệ –