How to handle 404 as json response if route not found?

151291

Using Laravel 5.4, I would like to send the json response, while there is no route found in api.php, how can I archive with in api.php and without Controller and Model class.

short: How to handle when the route name is incorrect?

Tried with fallback :

Route::fallback(function(){
    return response()->json(['message' => 'Not Found!'], 404);
});

Error for above :

Attribute [fallback] does not exist.

Please help me, how to handle Route not found exception when incorrect route name request.

NobbyNobbs

fallback available since version 5.6. try Route::any()


@NobbyNobbs - trim() expects parameter 1 to be string, object given

add at the end of your api.php (below all of the other routes) something like

Route::any('{path}', function() {
    return response()->json([
        'message' => 'Route not found'
    ], 404);
})->where('path', '.*');

how can I archive with in api.php and without Controller and Model class

In my opinion using of anonymous function as route handler it's not a best idea, because you can't cache your routes if you're using closures and it causes some overhead.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Nuxt.js and handle API 404 response for dynamic pages

分類Dev

Laravel '404 not found' error even after having a proper route?

分類Dev

Sending JSON response in Flask "POST" Route

分類Dev

Rest API response 404 Not found when run project with Java Application

分類Dev

Angular 6 ServiceWorker - index.html returned response 404 Not Found

分類Dev

Web API JSON resulting in 404 Not Found

分類Dev

How handle PATCH method in Flask route as API?

分類Dev

How to handle JSON data?

分類Dev

How to create and handle response for Ajax call

分類Dev

How to solve Route not found error in Laravel?

分類Dev

How to handle Dynamic JSON in Retrofit?

分類Dev

i18next load json error(404 Not Found)

分類Dev

Calling Web API 2.0 action with [Route] attribute with optional parameter returns 404 not found?

分類Dev

How to get Flask/Gunicorn to handle concurrent requests for the same Route?

分類Dev

How to return Guzzle JSON response

分類Dev

How to handle a skewed response in H2O algorithms

分類Dev

How do I return 404 response if no URL Rewrite Rules are matched

分類Dev

404 response appended to webapi response

分類Dev

MVC 6 404 Not Found

分類Dev

GraphiQL 404 page not found

分類Dev

Gitweb - 404 - No projects found

分類Dev

404 Not Found nginx React

分類Dev

How do you force a JSON response on every response in Laravel?

分類Dev

RestAssured: How to check the length of json array response?

分類Dev

How to use ajax response JSON data?

分類Dev

PHP: How to json encode of hindi language in response

分類Dev

How to compare XML response with Json in Karate

分類Dev

How to see axios error response JSON in React

分類Dev

How to properly parse this JSON response with Volley in Kotlin

Related 関連記事

  1. 1

    Nuxt.js and handle API 404 response for dynamic pages

  2. 2

    Laravel '404 not found' error even after having a proper route?

  3. 3

    Sending JSON response in Flask "POST" Route

  4. 4

    Rest API response 404 Not found when run project with Java Application

  5. 5

    Angular 6 ServiceWorker - index.html returned response 404 Not Found

  6. 6

    Web API JSON resulting in 404 Not Found

  7. 7

    How handle PATCH method in Flask route as API?

  8. 8

    How to handle JSON data?

  9. 9

    How to create and handle response for Ajax call

  10. 10

    How to solve Route not found error in Laravel?

  11. 11

    How to handle Dynamic JSON in Retrofit?

  12. 12

    i18next load json error(404 Not Found)

  13. 13

    Calling Web API 2.0 action with [Route] attribute with optional parameter returns 404 not found?

  14. 14

    How to get Flask/Gunicorn to handle concurrent requests for the same Route?

  15. 15

    How to return Guzzle JSON response

  16. 16

    How to handle a skewed response in H2O algorithms

  17. 17

    How do I return 404 response if no URL Rewrite Rules are matched

  18. 18

    404 response appended to webapi response

  19. 19

    MVC 6 404 Not Found

  20. 20

    GraphiQL 404 page not found

  21. 21

    Gitweb - 404 - No projects found

  22. 22

    404 Not Found nginx React

  23. 23

    How do you force a JSON response on every response in Laravel?

  24. 24

    RestAssured: How to check the length of json array response?

  25. 25

    How to use ajax response JSON data?

  26. 26

    PHP: How to json encode of hindi language in response

  27. 27

    How to compare XML response with Json in Karate

  28. 28

    How to see axios error response JSON in React

  29. 29

    How to properly parse this JSON response with Volley in Kotlin

ホットタグ

アーカイブ