aesircloud/laravel-actions
最新稳定版本:1.1.1
Composer 安装命令:
composer require aesircloud/laravel-actions
包简介
A simple Actions package for Laravel with job/queue/controller support.
README 文档
README
A simple actions package for Laravel.
Requirements: PHP 8.4+ and Laravel 12.
Features
- Single-Class Workflow: Write your logic once in a single
Actionclass. 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
Actionas 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
Installation
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,
PUBLISHING STUBS
To customize the stub files used for scaffolding, publish the package stubs:
php artisan vendor:publish --tag=actions-stubs
Usage
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.
Basic Example
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... } }
Running Synchronously
You can run the action synchronously by calling the run method on the action class:
$user = CreateUser::run($data);
Running as a Job
You can run the action as a job by calling the dispatch method on the action class:
$pending = CreateUser::dispatch($data);
Running as an Invokable Controller
// 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);
Running as a Multi-Method Controller
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']);
Security
If you've found a bug regarding security please mail security@aesircloud.com instead of using the issue tracker.
LICENSE
The MIT License (MIT). Please see License file for more information.
统计信息
- 总下载量: 30
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-28