Git Product home page Git Product logo

filament-api-service's Introduction

A simple api service for supporting filamentphp

Total Downloads

Installation

You can install the package via composer:

composer require rupadana/filament-api-service

Usage

php artisan make:filament-api-service BlogResource

Add this code to your routes file, example in routes/api.php

...
use App\Filament\Resources\BlogResource\Api;
...

BlogApiService::routes();

and then you will got this routes:

  • [GET] '/api/blogs' - Return LengthAwarePaginator
  • [GET] '/api/blogs/1' - Return single resource
  • [PUT] '/api/blogs/1' - Update resource
  • [POST] '/api/blogs' - Create resource
  • [DELETE] '/api/blogs/1' - Delete resource

On CreateHandler, you need to be create your custom request validation.

Im using "spatie/laravel-query-builder": "^5.3" to handle query and filtering. u can see "spatie/laravel-query-builder": "^5.3" https://spatie.be/docs/laravel-query-builder/v5/introduction

You can specified allowedFilters and allowedFields in your model

Example

class User extends Model {
    public static array $allowedFields = [
        'name'
    ];

    public static array $allowedSorts = [
        'name',
        'created_at'
    ];

    public static array $allowedFilters = [
        'name'
    ];
}

Create a Handler

To create a handler you can use this command. By default, i'm using CreateHandler

php artisan make:filament-api-handler BlogResource

or

php artisan make:filament-api-handler Blog

Transform API Response

php artisan make:filament-api-transformer Blog

it will be create BlogTransformer in App\Filament\Resources\BlogResource\Api\Transformers

next step you need to edit & add it to your Resource

    use App\Filament\Resources\BlogResource\Api\Transformers\BlogTransformer;

    class BlogResource extends Resource
    {
        ...
        public static function getApiTransformer() 
        { 
            return BlogTransformer::class; 
        }
        ...
    }

Group Name & Prefix

You can edit prefix & group route name as you want, default im use model singular label;

    class BlogApiService extends ApiService
    {
        ...
        protected static string | null $groupRouteName = 'myblog';
        ...
    }

How to secure it?

Basically, when u register the ApiService to the routes/api.php you can group it using sanctum middleware, Whichis this is default api authentication by Laravel. Read more about laravel sanctum

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

filament-api-service's People

Contributors

dependabot[bot] avatar paulovnas avatar rupadana avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.