Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Laravel API Backend

Carwyn Stephen edited this page Feb 18, 2018 · 2 revisions

Repositories

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.

Service Layer

For the back-end features like login/signup 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

API Authentication

The signup 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.

Clone this wiki locally