Support the ongoing development of Laravel.io →
IOC Architecture
Last updated 2 years ago.
0

Something i did was define a method in the base controller and call it from my other controllers. I think it's pretty straightforward.

0

astroanu said:

Something i did was define a method in the base controller and call it from my other controllers. I think it's pretty straightforward.

Right, it is straightforward. While I try to figure out a way without modify any controllers in "Laravel way".

0

It is hard to extend Illuminate\Http\Response.

I try to use the middleware

public function handle($request, Closure $next)
{
    $response = $next($request);
    if ($response->headers->contains('Content-Type', 'application/json')) {
        $result = [
            'status'=>'YES',
            'data'=>json_decode($response->getOriginalContent()->toJson())
        ];
        $response->setContent($result);
    }
    return $response;
}

Note sending $response->getOriginalContent() to array directly may lost nesting objects' data.

It is inefficient for convering json too many times.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

zerozh zerozh Joined 3 Sep 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.