panthro
panthro

Reputation: 24061

Creating a website that uses WEB and API routes in Laravel?

As I understand it, I could use API routes and build an API for my website's vue components to access data from my database and if I ever went on to build a mobile app (which is 50/50 at this stage), that would use the API too.

If this is the case, how would my website access the data - I presume through web routes, but would you not end up coding the same methods twice?

For example, a simple get users method, you would have one for the API that returned JSON and one for the website that returned a view. Would you have to write (I know it's simple eloquent but anyway...) the get users method twice in two controllers?

Looking for some guidance on basic setup of routes / controllers when using both WEB & API routes.

Upvotes: 0

Views: 332

Answers (1)

Akhzar Javed
Akhzar Javed

Reputation: 628

We use repository pattern in laravel to avoid adding logics in controller or in models

You can use the repository as data layer and change what you want to pass on ApiUserController or UserController. (Json or Array)

Web routes can also return json though. So using web route for vue and API routes for mobile app is also an option

Or you can use just API routes for both.

Depends on use case

Repository Pattern Article

https://itnext.io/repository-design-pattern-done-right-in-laravel-d177b5fa75d4

Upvotes: 1

Related Questions