Hi Guys, I gonna create website with laravel and use the same rest API for mobile App. Is there any idea without using two controllers for web and API. Please suggest whether I have to go with angular or react for front end otherwise laravel web view
There are many ways to do it, but I could share 2 things I did before.
First way to do it:
create a duplicate route that points to the same controller, i.e: api.domain.com/auth/login and domain.com/login
but pointing those routes into 1 controller@action and inside the action, if the request()->wantsJson() serve a json response, else serve the view-blade.
Second way to do it:
By combiningapi and web middleware group into one
Now your url will serve 2 type of responses domain.com/login
apply the same thing, if the request wants a json, return json, else return view-blade
CONS:
-- I encountered an issue when using Passport with this combination of middleware groups
TL;DR:
Yes you can, as long as your controller's action detects the wanted request.
@kaviadagency You're going to need an API for a mobile app, no two ways about it. How you build your web app is up to you.
You can have separate controllers for your web app and that way you can use your models and database directly and not be restricted to what endpoints your API exposes and what fields are returned in responses. Or, you can be a complete hipster, build your API and then have an JavaScript-based SPA (single-page application) that consumes that same API.
That means that any time you update your API, you have to update every client that uses it. So make sure you take time to design your API upfront. An API should always be backwards-compatible, i.e. never introduce breaking changes, because those breaking changes will propagate to every client using it.