Hi,
Sorry for my english first, I'm not used to write in english.
I explain my problem. I used on Laravel 4, the method 'App:abort' to send a answer when there was a error. I use Laravel as a API. Now I use Laravel 5 and I would like to send a error with the JSON format. But I use 'abort()' and it send me a message with an HTML form and I would like to send a message like 'App::abort()' did it. { 'error': 404, 'message': 'Not found' }
Can someone explain how to do it ? I saw the Handler but I don't understand how to use it.
Thanks you
Why not simply say
return response()->json([ 'error': 404, 'message': 'Not found' ]);
@astroanu : It is not really a solution because it is not a HTTP error. The answer status will be 200 and not 404 here. And I would like that the answer will be 404 and not 200.
You can pass the http code on to the json method as follows:
return response()->json([ 'error': 404, 'message': 'Not found' ], 404);
From the Laravel documentation
json(string|array $data = array(), int $status = 200, array $headers = array(), int $options)
Actually i misread your first post. On L5 you can simply do this on your controller.
abort(404, 'Doomed');
Thank you very much this worked for me...
astroanu said:
You can pass the http code on to the json method as follows:
return response()->json([ 'error': 404, 'message': 'Not found' ], 404);
From the Laravel documentation
json(string|array $data = array(), int $status = 200, array $headers = array(), int $options)
astroanu said:
You can pass the http code on to the json method as follows:
return response()->json([ 'error': 404, 'message': 'Not found' ], 404);
From the Laravel documentation
json(string|array $data = array(), int $status = 200, array $headers = array(), int $options)
I think you meant to put hash rockets :)
return response()->json([ 'error' => 404, 'message' => 'Not found' ], 404);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community