henninghorn's avatar

Adding data to response after returning it in controller

In my controllers I'm returning arrays of data.

public function index()
{
    return $this->repo->paginate(25);
}

I want to add content to the above response, ie. if I had a debug parameter present, it should add a _debug array to the response through some kind of array_merge thing.

Is it possible to add anything to the response through middleware or something else, when you're returning data from the controller?

0 likes
2 replies
pmall's avatar
pmall
Best Answer
Level 56

Use an after middleware.

After middleware allows to manipulate request after it has been processed and returned by the controller action.

henninghorn's avatar

Ah, thank you very much @pmall

I completely overlooked it.

I was looking at terminable middleware, but that wouldn't work because it runs "after the HTTP response has already been sent to the browser"

Please or to participate in this conversation.