avstriyskiy/laravel-api-controller-traits

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

Traits for easy building API controllers for your project Front-End.

1.0.1 2023-09-29 21:19 UTC

This package is auto-updated.

Last update: 2024-04-11 12:29:11 UTC


README

This package allows you to use basic Index, Get, Create, Update and Delete methods based on traits. Also package provides an abstract class that using all these traits, so you can just simply extend this class. You can customize Response, Resources and Collections just by implementing methods in your controller.

Basic usage

Here is an example of controller which just extends APIController class. It contains all methods. You must declare public getModelClass() method which returns Model class name.

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Avstriyskiy\LaravelApiControllerTraits\APIController;

class UserController extends APIController
{
    public function getModelClass(): string
    {
        return User::class;
    }
}

If you don't need all methods you can use traits in your controller like in this example. You still have to declare public getModelClass() method.

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Avstriyskiy\LaravelApiControllerTraits\Methods\IndexMethod;
use Avstriyskiy\LaravelApiControllerTraits\Methods\GetMethod;
use Avstriyskiy\LaravelApiControllerTraits\Methods\CreateMethod;
use Avstriyskiy\LaravelApiControllerTraits\Methods\UpdateMethod;
use Avstriyskiy\LaravelApiControllerTraits\Methods\DeleteMethod;

class UserController extends Controller
{
    use IndexMethod, GetMethod, CreateMethod, UpdateMethod, DeleteMethod;

    public function getModelClass(): string
    {
        return User::class;
    }
}

Defining requests

You have to create request

List of available methods

Available customization

If you need any additional options for your controller, you can just declare methods.

getRelations()

Method allows you to load relations for your model when preparing Resource.
By default it returns an empty array.

class UserController extends APIController
{
    ...
    public function getRelations(): array
    {
        return ["phones", "comments"];
    }
}
getResource()

Method allows you to define custom Resource to be used for response in Get, Create and Update Methods.
By default it returns a JsonResource::class

class UserController extends APIController
{
    ...
    public function getResource(): string
    {
        return UserResource::class;
    }
}
getCollection()

Method allows you to define custom Collection to be used for response.
By default it returns a ResourceCollection::class

class UserController extends APIController
{
    ...
    public function getCollection(): string
    {
        return UserCollection::class;
    }
}
getModelNotFoundExceptionClass()

Method allows you to define custom Exception class to be used when model is not found in DB.
By default it returns an IlluminateModelNotFoundException::class

class UserController extends APIController
{
    ...
    public function getModelNotFoundExceptionClass(): string
    {
        return IlluminateModelNotFoundException::class;
    }
}
additionalDataFor%METHOD%()

You can add an additional data for your JsonResponse simply defining this kind of methods returnin array of additional data.
By default they return an empty array.

class UserController extends APIController
{
    ...
    public function addditionalDataForIndex(): array
    {
        return ["statusText" => "Success"];
    }

    public function additionalDataForCreate(): array
    {
        return ["statusText" => "Created"];
    }
}
resourceClassFor%METHOD%()

Also you can define custom resource/collection class for single method.
Signature is the same for IndexMethod for more compitability, but it should return a collection class like in example.
By default these methods return getResource() or getCollection() methods.

class UserController extends APIController
{
    ...
    public function resourceClassForIndex(): string
    {
        return UserCollection::class;
    }

    public function resourceClassForGet(): string
    {
        return UserResource::class;
    }
}
getModelNotFoundExceptionClassFor%METHOD%()

Method allows you to define different custom Exception class to be used when model is not found in DB.
By default it returns a getModelNotFoundException()

class UserController extends APIController
{
    ...
    public function getModelNotFoundExceptionClassForGet(): string
    {
        return GetModelNotFoundException::class;
    }
    public function getModelNotFoundExceptionClassForDelete(): string
    {
        return $this->getMOdelNotFoundExceptionClass();
    }
}