This package simplifies creating api (json) endpoints for your laravel application.
kaydomrose/laravel-api-action is a Laravel package for this package simplifies creating api (json) endpoints for your laravel application. .
It currently has 0 GitHub stars and 0 downloads on Packagist.
Install it with composer require kaydomrose/laravel-api-action.
Discover more Laravel packages by kaydomrose
or browse all Laravel packages to compare alternatives.
Last updated
This package simplifies creating api (json) endpoints for your laravel application by reducing routing, authorization, validation and controller logic to a single class.
class UserReadAction extends ActionController
{
public function method(): string
{
return 'GET';
}
public function uri(): string
{
return '/users/{user}';
}
public function allow(Request $request): bool
{
return true;
}
public function rules(Request $request): array
{
return [];
}
public function handle(User $user): User
{
return $user;
}
}
This will action will
/users/{user}Via Composer
$ composer require kaydomrose/laravel-api-action
First, create an action via artisan command.
$ php artisan make:action MyAction
This creates a new class app/Http/Actions/MyAction.php.
Next, register your action in your routes/api.php
LaravelApiAction::routes([
MyAction::class,
]);
Done!
public function method(): string {
return 'GET';
}
public function uri(): string {
return '/users/{user}';
}
Specify http verb and uri for your action.
See https://laravel.com/docs/master/routing#available-router-methods for available verbs.
Above example will be translated to
Route::get('/users/{user}', /* ... */);
public function allow(Request $request): bool
{
return true;
}
Check whether the current request is authorized or not.
Works just like form request authorization.
public function rules(Request $request): array
{
return [];
}
Specify rules for request validation as seen in form builder validation.
public function handle() {
/* Your logic */
}
This is where you put your action logic, just like you would normally do in your controller.
Also supports dependency injection and route model binding.
Your return value will be wrapped with response()->json(), so don't wrap it yourself.
Just return raw data.
public function responseStatusCode(): int {
return 200;
}
Customize response status code.
Default is 200.
public function validationMessages(): array {
return [];
}
Customize validation error messages.
Default is [].
Please see the changelog for more information on what has changed recently.
$ composer test