jamosaur/foundation

There is no license information available for the latest version (0.5) of this package.

Foundation for API's in Laravel

0.5 2024-04-29 14:29 UTC

This package is auto-updated.

Last update: 2024-10-29 15:46:19 UTC


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

  1. composer require jamosaur/foundation
  2. Update your API controllers to extend Jamosaur\Foundation\ApiController. This extends the default Laravel controller but also adds more methods that we will use.
  3. Update your API middleware in app\Http\Kernel.php to use Jamosaur\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 extend League\Fractal\TransformerAbstract