A simple actions package for Laravel.
Requirements: PHP 8.4+ and Laravel 12.
- Single-Class Workflow: Write your logic once in a single
Action
class. No need to duplicate code in controllers, jobs, or other classes. - Run Synchronously or as a Queued Job
- Call
MyAction::run($data)
for immediate, synchronous execution. - Call
MyAction::dispatch($data)
to run the action as a queued job.
- Call
- Controller Integration
- Use your
Action
as an invokable controller with __invoke(). - Or define multiple methods (e.g., index, store) and treat it like a standard Laravel controller.
- Override
asController()
to parse/validate request data before callinghandle()
.
- Use your
You can install the package via composer:
composer require aesircloud/laravel-actions
Laravel’s package auto-discovery will register the service provider automatically. If you need to manually register it, add the following to your config/app.php
providers array:
AesirCloud\LaravelActions\Providers\ActionServiceProvider::class,
To customize the stub files used for scaffolding, publish the package stubs:
php artisan vendor:publish --tag=actions-stubs
To scaffold a new action, run the following command:
php artisan make:action {ActionName}
This will create a new action class in the app/Actions
directory.
php artisan make:action CreateUser
Creates an action file under app/Actions/CreateUser.php
.
namespace App\Actions;
use AesirCloud\LaravelActions\Action;
class CreateUser extends Action
{
public function handle()
{
// Your logic here...
}
}
You can run the action synchronously by calling the run
method on the action class:
$user = CreateUser::run($data);
You can run the action as a job by calling the dispatch
method on the action class:
$pending = CreateUser::dispatch($data);
// app/Actions/MyAction.php
namespace App\Actions;
use AesirCloud\LaravelActions\Action;
use Illuminate\Http\Request;
class MyAction extends Action
{
public function handle(): mixed
{
return 'Hello world!';
}
public function asController(Request $request): mixed
{
// e.g., $data = $request->validate([...]);
return $this->handle();
}
}
// routes/web.php
use App\Actions\MyAction;
use Illuminate\Support\Facades\Route;
Route::get('/my-action', MyAction::class);
namespace App\Actions;
use AesirCloud\LaravelActions\Action;
use Illuminate\Http\Request;
class MyAction extends Action
{
public function handle(): mixed
{
return 'Default logic (if you still want to call it externally).';
}
// Typical controller method:
public function index(Request $request): mixed
{
return 'Called via index method!';
}
public function store(Request $request): mixed
{
return 'Called via store!';
}
}
// routes/web.php
Route::get('/my-action', [MyAction::class, 'index']);
Route::post('/my-action', [MyAction::class, 'store']);
If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License file for more information.