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

Jonjie's avatar
Level 12

API vs Web Route when creating SPA Application

I'm just wondering what is the best route to use when we use SPA approach with Laravel.

  1. Should I use web routes or api routes?
  2. Considering that it will also have a mobile app, so automatically I also have to create APIs for the mobile, so obviously I should use the api route it?
  3. Will it not look messy if I combine the APIs for my SPA, and the APIs for mobile app?
  4. Regarding authentication, it is automatically a stateless app so I have to also generate a token. Where should I save it, database or localStorage?

P.S. I've already read this blog https://laravel-news.com/using-vue-router-laravel

0 likes
3 replies
thewebartisan7's avatar

You should separate web and api routes since they use different middleware and authorizations.

But for mobile app and SPA, or anything else that use API, you can have one single API. The authorizations change for mobile app compared to SPA (Statefull vs Stateless). I suggest to check Laravel Sanctum https://laravel.com/docs/7.x/sanctum which can be used for both mobile app and SPA. The docs should give you answers to all your questions.

There is also Laravel Passport, but is more complicated and I think for your case is enought Sanctum, see https://laravel.com/docs/7.x/passport

Jonjie's avatar
Level 12

@thewebartisan7 I'm confused. The use of passport is for OAuth and it is different from sanctum right? Please correct me if I'm wrong.

Kripton's avatar

Yes, if you need OAuth you should use Passport. Sanctum uses a different (and less complex) approach. Note that, regarding question 4, Sanctum can actually make stateful authentication for SPAs.

Please or to participate in this conversation.