jamosaur / foundation
Foundation for API's in Laravel
Installs: 2 265
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.0|^8.1|^8.2
- illuminate/http: ^8.0|^9.0|^10.0|^11.0
- illuminate/routing: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
- spatie/laravel-fractal: ^6.0
README
What is this?
This is the base that I like to use for constructing API's with Laravel.
It is essentially a wrapper for spatie's laravel-fractal.
Installation
composer require jamosaur/foundation
- Update your API controllers to extend
Jamosaur\Foundation\ApiController
. This extends the default Laravel controller but also adds more methods that we will use. - Update your API middleware in
app\Http\Kernel.php
to useJamosaur\Foundation\Middleware\ApiRequestMiddleware
Example Usage
Controller
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\JsonResponse; use Jamosaur\Foundation\ApiController; class UserController extends ApiController { public function index(): JsonResponse { $users = User::all(); // By default this will try to find a transformer in `App\Transformers` called // `UserTransformer`. It guesses the name of the transformer based on the controller // name. return $this->transformCollection('users', $users) ->respond(); } public function definedTransformer(): JsonResponse { $users = User::all(); // You can override the transformer to use like this. return $this->setTransformer(new CustomTransformer()) ->transformCollection('users', $users) ->respond(); } }
Important Notes
- Transformers MUST implement
Jamosaur\Foundation\Contracts\TransformerContract
and extendLeague\Fractal\TransformerAbstract