Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yougotnet's avatar

Is it possible to use both web routes and api routes together?

I'm trying to implement JWT-Auth for api routes on a mobile app but keep the standard web routes for the website application.

The quick start guide for JWT-Auth implies to only use api routes.

Is this doable?

0 likes
4 replies
schachfeld's avatar

Of course! I've also done this in my application. You can have as many paths in as many files as you want.

I assume, that you want to have the API routes in a different file than the Web routes. In the RouteServiceProvider you can define which files, namespaces our middlewares or whatever you want to use.

Normally Laravel even comes with an api file in the folder routes. You can specify your JWT middleware to be on all api routes and keep your setup for the web routes like before.

yougotnet's avatar

Do you know of a good write up of how to implement the JWT on api routes and keep the web routes?

I read through some blogs and all of them seem to make everything api; I am struggling on getting it to work.

When I post to /api/login I get a MethodNotAllowedException.

schachfeld's avatar

Hey, sorry I forgot about that thing. But I think i know what your problem is. Maybe you have a case somewhere in your web routes where the route /{any} or something like that gets caught. There are two solutions: Number one is that you exclude "api" via regex. Number two is that you register api routes before web routes in your RouteServiceProvider. Let me know if you used a solution of that or if you already got a different solution :)

yougotnet's avatar

After doing some research, I realized that I needed to use the api.php routes file versus the web.php routes file.

Then next challenge was validating user session because api doesn't use sessions and so I ended up using JWT.

Then next challenge after that was, how can I use the same set of Controllers for CRUD; without creating controllers for api versus web. The solution I used was in my BaseController, I inspected the route and set a variable $guard to either api or web. Then in my CRUD function I can validate auth by auth($this->guard)->check(). I can also have different return types based on the guard.

Everything is working for me, not sure if it the right or best approach but I couldn't find a better one.

Please or to participate in this conversation.