mercredi 30 octobre 2019

Reuse controller action but modify response in Laravel

Assume I have a base controller which return json response as

class BaseController extends Controller {

    public function test(Request $request) 
    {
        ..
        $data = ...
        return response()->json(['data' => $data);
    }          
}

Now I have another controller which extends from BaseController and want to reuse the test action

class MyController extends BaseController {

    public function test(Request $request) 
    {
        ..
        $data = parent::test($request);
        $data = $data->getData();

        $data->foo = 'bar'; // customize

        return response()->json(['data' => $data);
    }          
}

Are there any better way to rewrite the above codes?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/31Tq6IY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire