Installation

composer require phpsa/laravel-api-controller

Publish Configuration File

(Optional) publish the configuration file if you need to change any of the default configurations.

php artisan vendor:publish --provider="Phpsa\LaravelApiController\ServiceProvider" --tag="config"

(Optional) Add the following middleware to your api settings in your app/Http/Kernel.php file:


    protected $middlewareGroups = [
        'web' => [
           ...
        ],

        'api' => [
            \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Phpsa\LaravelApiController\Http\Middleware\SnakeCaseInputs::class
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

this will automatically convert all camelCase variables to snake-case for you on incomming requests.

Configuration variables

return [
    // Relative path from the app directory to api controllers directory.
    'controllers_dir' => env('PHPSA_API_CONTROLLER_DIR', 'Http/Controllers/Api'),
    // Relative path from the app directory to the api routes file.
    'routes_file' => '../routes/api.php',
    // Relative path from the app directory to the models directory. Typically it's either 'Models' or ''.
    'models_base_dir' => 'Models',
    // Relative path from the base directory to the api controller stub.
    'controller_stub' => env('PHPSA_API_CONTROLLER_STUB', 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/controller.stub'),
    // Relative path from the base directory to the route stub.
    'route_stub' => env('PHPSA_API_ROUTE_STUB', 'vendor/phpsa/
    laravel-api-controller/src/Generator/stubs/route.stub'),

    'parameters' => [
        'include' => 'include', // which hasOnes / HasMany etc to include in the response
        'filter' => 'filter', // filter on fields
        'sort' => 'sort', // sort the response
        'fields' => 'fields', // fields to return
        'page' => 'page', //Page number when pagination is on
        'group' => 'group', // Group by query
        'addfields' => 'addfields', //Add fields to the default fields
        'removefields' => 'removefields', //Remove fields from the default fields
        'limit' => 'limit', // how many records to return
    ],
];