Support the ongoing development of Laravel.io →
Requests Eloquent Architecture
Last updated 1 year ago.
0

There is no much different between mobile REST API and web application. It is only in the response that the controller returns as result. For web application, a view is returned while for the mobile application, a json result is returned.

Using that concept, I can think of two approaches of handling your scenario.

#APPROACH 1 Obviously, the better one since it avoids duplication of code, Maintain the same routes for both mobile and web application, however, add an identifier for the source of the request, i.e "mobile" or "web". This parameter will help you to decide whether to send the response as json or view. In the controller, retrieve the value of the identifier, then based on the value of the identifier.

Pseudocode

if( identifier == "web"){
return view("path to view'') e.g return view('backend.access.create')
}
else{
return response()->json(array_here); e.g return response()->json(['name' => 'Abigail', 'state' => 'CA']);
}

#APPROACH TWO You will have to work on two places. Create a different sets of routes and controllers for the REST API from those of the web application. Routing

  • Create a different group of routes for the mobile application REST API. The controller for the mobile api route will simply return json while the one for the web application will return view like in the first approach. Of course this introduces the problem of code duplication, but you can solve that by extracting the similar parts of code in one place.

But overall, APPROACH 1, is the alternative I would go with.

quyle92 liked this reply

1

@vitasiku: Would you plz give me an Idea how can I add a identifier for web view. Actually where I should add this identifier and how I can get them in controller. Plz, give me a details on this topics. I going through hard time wiith this issue. thank you

Last updated 7 years ago.
0

Yes, same here. i am facing a lots of problem regarding the web.php and api.php in laravel 5.4, as i want to access the route in both web as well as in mobile, I can do it in json but can not do to make it view in blade. Hope, identifier is the option, would you please tell us briefly. Thanks

0

Sign in to participate in this thread!

Eventy

Your banner here too?

yasithacp yasithacp Joined 12 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.