-
Notifications
You must be signed in to change notification settings - Fork 42
Laravel API Backend
I've used Eloquent to make the models in App/Models
, but throughout the app I interact with the data layer through the repositories I've created for every model. I've always liked this extra layer of abstraction from the data layer as I feel repositories are a lot easier to mock for testing. You can find the interfaces for the repositories in app/Contracts/Repository
, and I've written implementation repositories for Eloquent in App/Repositories/Eloquent
.
For the back-end features like login/# etc. I've decided to use the service layer pattern to keep this functionality out of the controllers. This keeps the logic for doing this in a central place that can be used from anywhere instead of straight in one controller. These services can be found in app/services
. When using these services in your new code, you can type hint the service in the constructor arguments, and Laravel's container will resolve a singleton of that service for you. You can see where I've created the bindings for the services in App/Providers/ServiceLayerServiceProvider.php
The # and login services both return a response with a cookie attached, this cookie is made using Laravel Passport's ApiTokenCookieFactory. The front end then sends this cookie with every request along with a CSRF token, this authenticates the user for routes wrapped in the API auth middleware.