Filament API Service
A simple API service for supporting FilamentPHP
Last updated
A simple API service for supporting FilamentPHP
Last updated
You can install the package via composer:
Register it to your filament Provider
Since version 3.0, routes automatically registered. it will grouped as '/api/admin
'. admin
is panelId. to disable panelId prefix, please set route.panel_prefix
to false
So, You don't need to register the routes manually.
The routes will be :
GET
/api/admin
/blogs
Return LengthAwarePaginator
GET
/api/admin
/blogs/1
Return single resource
PUT
/api/admin
/blogs/1
Update resource
POST
/api/admin
/blogs
Create resource
DELETE
/api/admin
/blogs/1
Delete resource
On CreateHandler, you need to be create your custom request validation.
By default, Token resource only show on super_admin
role. you can modify give permission to other permission too.
Token Resource is protected by TokenPolicy. You can disable it by publishing the config and change this line.
[!IMPORTANT] If you use Laravel 11, don't forget to run
php artisan install:api
to publish the personal_access_tokens migration after that runphp artisan migrate
to migrate the migration, but as default if you run thephp artisan install:api
it will ask you to migrate your migration.
We used "spatie/laravel-query-builder": "^5.3"
to handle query selecting, sorting and filtering. Check out the spatie/laravel-query-builder documentation for more information. You can specified allowedFilters
and allowedFields
in your model. For example:
To create a handler you can use this command. By default, i'm using CreateHandler
or
it will be create BlogTransformer in App\Filament\Resources\BlogResource\Api\Transformers
next step you need to edit & add it to your Resource
You can edit prefix & group route name as you want, default this plugin use model singular label;
You can add or override middlewares at two specific places. Via the Filament Panel Provider and/or via the Resources $routeMiddleware.
If you set route.use_resource_middlewares
to true, the package will register the middlewares for that specific resource as defined in:
Then your API resource endpoint will go through these middlewares first.
Another method of adding/overriding middlewares is via the initialization of the plugin in your Panel Provider by adding the middleware()
method like so:
When you want to enable Tenancy on this package you can enable this by setting the config tenancy.enabled
to true
. This makes sure that your api responses only retreive the data which that user has access to. So if you have configured 5 tenants and an user has access to 2 tenants. Then, enabling this feature will return only the data of those 2 tenants.
If you have enabled tenancy on this package but on a specific Resource you have defined protected static bool $isScopedToTenant = false;
, then the API will honour this for that specific resource and will return all records.
If you want to make api routes tenant aware. you can set tenancy.awareness
to true
in your published api-service.php. This way this package will register extra API routes which will return only the specific tenant data in the API response.
Now your API endpoints will have URI prefix of {tenant}
in the API routes when tenancy.awareness
is true
.
It will look like this:
Overriding tenancy ownership relationship name by adding this property to the Handlers protected static ?string $tenantOwnershipRelationshipName = null;
Since version 3.0, it will automatically detect routes and secure it using sanctum.
To Generate Token, you just need create it from admin panel. It will be Token Resource there.
Set API to public by overriding this property on your API Handler. Assume we have a PaginationHandler
The MIT License (MIT).