Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Response(JSON and webpage) in API and Website for Laravel 404 and 500?

I want to show the different response for API and website. In api response I want to show json response with 404 and 500 for type of exception mainly for routes.

If a user try to request a route and route not found I want to show a response in json response for API and webpage for website.

I know and try the code into app/Exceptions/Handler.php

public function render($request, Exception $exception)
{
    if ($exception instanceof NotFoundHttpException) {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Not Found'], 404);
        }
        return response()->view('404', [], 404);
    }
    return parent::render($request, $exception);
}

https://laravel.com/docs/5.4/errors#http-exceptions

but failed can anybody help me how can I set different responses for error pages.

like image 675
Prashant Barve Avatar asked Apr 27 '17 06:04

Prashant Barve


People also ask

What is Laravel response JSON?

Introduction to Laravel Response JSON. Laravel Response JSON, the Laravel framework has been in demand for the last few years. It has been able to garner a sizeable portion of the development framework market. The reasons are plenty. One of the most important features of the framework has been its expressive command line methods.

Should you use JSON for 404 routes in an API?

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. Here’s what you get if you request an undefined route with a JSON content type:

How to handle error messages in Laravel with web and API?

If you’re building a Laravel project with both Web and API sides, you need to customize error messages for each of them separately. In web-view there should be error pages, and API exceptions should return JSON with status codes. How to handle it? I will show you an example with case of Model Not Found 404.

What are special responses in Laravel Jason?

Special responses come in the form of JSON responses. Explanation: Looking at the vast amount of examples, it is quite certain that the laravel Jason response primarily works as an output query which when mixed with parameters provides us with customized queries.


1 Answers

Expects JSON is about headers, i do not like that solution for API errors, you can access the API through a browser. My solution is most of the times to filter by the URL route, because it starts with "api/...", which can be done like so $request->is('api/*').

If you have your routes that are not prefixes with /api, then this will not work. Change the logic to fit with your own structure.

public function render($request, Exception $exception)
{
    if ($exception instanceof NotFoundHttpException) {
        if ($request->is('api/*')) {
            return response()->json(['error' => 'Not Found'], 404);
        }
        return response()->view('404', [], 404);
    }
    return parent::render($request, $exception);
}
like image 56
mrhn Avatar answered Oct 17 '22 17:10

mrhn