Search code examples
phplaravelapi-design

How to organize laravel Controllers when using API


I have some data that requires to be authenticated (web auth middleware) to fetch it,

I'm using ajax request to fetch the data and it's fine.

But when building an API Should I create a new method to return the same data and use (jwt-auth middleware for example)

or Just use what I have created before ?.

And if the answer is use what you have created .., How to use (jwt-auth middleware) for API and (web auth middleware) for web in the same Controller ?


Solution

  • You could have 2 guard for login such as web and api guards that authenticate users. web for session and api use jwt or passport. Then use web route file use auth:web middleware and in api route use auth:api so have 2 route different but same controller@method . pay attention just use json accept and return response don't return view in method