user image

Shilpa
Published in : 2022-02-27

How to use Laravel app as Rest API and as a regular website, both at a same time?

Laravel

I want my laravel app to run as a regular website as well as I want to use it as a Rest API.

My concern is about Guards.

I am not able to do both as default authentication and I need these two guards that my application work.

...'my_guards' => [ 'website' => [ 'driver' => 'session', 'provider' => 'usr', ], 'rest-api' => [ 'driver' => 'jwt', 'provider' => 'usr', 'hash' => true, ],],...

Any proper solution to achieve above requirements?

Comments

Rakshit Date : 2022-03-02

Best answers

34

Best answers

34

I don't really think there is a perfect answer to this, you want to decouple your APIs, that sounds like a great plan. It can be useful when your api gets heavier traffic.

 

As you want to run your API and web application at a same time, you would require to separate your application as

  1. Rest APIs - Use Ajax requests-response (You need to return response in the JSON format)
  2. Here you can return PHP views as usual.

But by doing this, you need to double work for both the approaches and so the maintenance will be there. So plan accordingly. I don't recommend decoupling APIs with rest and web for Single page applications.

Hope Ajax's request response will help you for sure.

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Increase Sanctum token length in Laravel x9
Publish date: 2022-07-16 | Comments: 2
Laravel API POST method returns 419
Publish date: 2022-08-03 | Comments: 1
Laravel install passport have access denied error
Publish date: 2022-02-23 | Comments: 4
How can i pass data from blade template to vue component?
Publish date: 2022-02-11 | Comments: 1
tymon/jwt-auth 0.5.12 requires illuminate/support ~5.0
Publish date: 2022-02-18 | Comments: 2