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
But overall, APPROACH 1, is the alternative I would go with.
quyle92 liked this reply
@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
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community