vendredi 25 août 2017

Laravel how to return JSON when not in controller?

Is there some way to return JSON from laravel by using something similar to abort() function.

From controller returning JSON is easy we can just call

return response()->json(['message' => 'my mesage'], 200);

But from some other place (custom class for example) to return JSON I have to create a return chain to the contoller.

Is there a way to return json without resorting to a return chain. Like using abort() function. I would like to use something similar to abort(400, 'my message') but insted of returning html like abort does return JSON.

or is there a way to overwrite abort to return JSON.

I would be thankfull solution that works for laravel 5.3 and up.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire