samedi 26 janvier 2019

Best way to write same code for both (web and API) with different type of response?

I am working on a laravel project where I have to write code for web-app as well as APIs with same business logic.

Since there is no difference in business logic so I am thinking to write code in traits and use them in different controllers and send response according to request headers. In this way same code will serve both web-app and mobile apps.

For now I check if request wants json using wantsJson() method of Request class.

if($request->wantsJson()){
     return response()->json(...);
}

return redirect()->route(...)->with('key','value'); 

Is there any better way to accomplish this ?




Aucun commentaire:

Enregistrer un commentaire