404 Responses in a Laravel API

A useful feature that shipped in Laravel 5.5 is fallback routing. You can learn about fallback routing in Better 404 responses using Laravel +5.5 by Mohamed Said (the author of the feature) to get the full picture of why its useful and how to use fallback routes. When you are creating an API, you probably want a 404 route that responds with JSON (or whatever format you are serving via content negotiation) instead of the default 404 JSON response. Heres what you get if you request an undefined route with a JSON content type: You can see that we get an empty message that isnt necessarily super helpful, but the framework returns valid JSON without any work on our part. Lets cover a couple of scenarios and walk through how you can ensure your API responds with a fallback 404 response with a useful message when an API route doesnt match. Using the Laravel CLI, well create a new project to walk through adding a 404 response to your API:

Read Full Article at https://laravel-news.com/404-responses-laravel-api

Tags: , , , , , , , , , , , ,