1

My controllers that respond to api request, all respond with data that should be transformed into JSON.

Instead of using json_encode() in every controller, I would like to use middleware to just wrap the response. Since these routes for the API are already protected with the auth:api middleware which from what I have read is defined within the core of Laravel.

How would I go about adding a method to that middleware that would do all the json processing for me? could

1

1 Answer 1

1

In the case of API's it's very common to return just a model or collection.

If you are doing that a return $someData in your API controller, $someData will automatically be converted to JSON and returned with the appropriate headers.

Middleware is unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.