cleaniquecoders/laravel-action
Composer 安装命令:
composer require cleaniquecoders/laravel-action
包简介
Simple Actionable for Laravel
README 文档
README
This package, Simple Actionable for Laravel, provides a structured way to manage action classes in your Laravel applications, making it easier to encapsulate business logic, transformations, and validations within reusable classes. The package utilizes lorisleiva/laravel-actions to offer extended functionality, enabling actions to be executed in multiple contexts (e.g., jobs, controllers, event listeners) and simplifying your codebase.
Installation
You can install the package via composer:
composer require cleaniquecoders/laravel-action
Features
This package builds on top of lorisleiva/laravel-actions, allowing you to:
- Create versatile action classes that can be executed as invokable objects, controllers, or dispatched as jobs.
- Use property setters to configure actions dynamically.
- Apply transformations like hashing or encryption to specific fields.
- Manage
updateOrCreatebehavior with constraints on unique fields.
Usage
You can create an action using the Artisan command:
php artisan make:action User\\CreateOrUpdateUser --model=User
This will create an action in app\Actions\User:
<?php namespace App\Actions\User; use App\Models\User; use CleaniqueCoders\LaravelAction\ResourceAction; class CreateOrUpdateUser extends ResourceAction { public string $model = User::class; public function rules(): array { return [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|unique:users,email', 'password' => 'required|string|min:8', ]; } }
New Features and Example Usage
1. Flexible Property Setter
You can set properties, such as hashFields, encryptFields, and constrainedBy, dynamically using the setProperty method:
$action = new CreateOrUpdateUser(['name' => 'John Doe', 'email' => 'johndoe@example.com', 'password' => 'secretpassword']); $action->setProperty('hashFields', ['password']); // Hash the password $action->setProperty('encryptFields', ['ssn']); // Encrypt SSN $action->setProperty('constrainedBy', ['email' => 'johndoe@example.com']); // Use email as a unique constraint
This flexible property setting reduces boilerplate and simplifies action configuration.
2. Field Transformation with Hashing and Encryption
The ResourceAction class supports field-level transformations. For example, you can hash a password field and encrypt an ssn field:
$inputs = [ 'name' => 'Jane Doe', 'email' => 'janedoe@example.com', 'password' => 'securepassword', 'ssn' => '123-45-6789', ]; $action = new CreateOrUpdateUser($inputs); $action->setProperty('hashFields', ['password']); $action->setProperty('encryptFields', ['ssn']); $record = $action->handle();
After execution:
- The
passwordfield will be hashed for security. - The
ssnfield will be encrypted, ensuring secure storage.
3. Constraint-Based updateOrCreate
Specify constraints to perform updateOrCreate actions based on unique fields or identifiers. Here’s an example of updating an existing user by id:
// Assume there's an existing user with this email $existingUser = User::create([ 'name' => 'Old Name', 'email' => 'uniqueemail@example.com', 'password' => Hash::make('oldpassword'), ]); // Define the inputs to update the existing user $inputs = [ 'name' => 'John Doe Updated', 'email' => 'uniqueemail@example.com', // Same email 'password' => 'newpassword', ]; $action = new CreateOrUpdateUser($inputs); $action->setProperty('constrainedBy', ['id' => $existingUser->id]); // Update by user ID $record = $action->handle(); // The existing user record with the specified ID will be updated.
This allows precise control over updateOrCreate behavior based on custom constraints.
Using lorisleiva/laravel-actions for Multi-Context Execution
With lorisleiva/laravel-actions, actions created with this package can be used in multiple contexts. You can run the action as:
- An Invokable Object:
$user = (new CreateOrUpdateUser(['name' => 'Jane', 'email' => 'jane@example.com']))->handle();
- A Controller:
Route::post('users', CreateOrUpdateUser::class);
- A Job:
CreateOrUpdateUser::dispatch(['name' => 'Jane', 'email' => 'jane@example.com']);
- An Event Listener:
Event::listen(UserRegistered::class, CreateOrUpdateUser::class);
Testing
Run the tests with:
composer test
Changelog
Please see CHANGELOG for more information on recent changes.
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.
cleaniquecoders/laravel-action 适用场景与选型建议
cleaniquecoders/laravel-action 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.45k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「cleaniquecoders」 「laravel-action」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cleaniquecoders/laravel-action 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cleaniquecoders/laravel-action 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cleaniquecoders/laravel-action 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generate running number when creating new records in your table.
Generate Database Schema Documentation
Keep track all the emails sent in the your Laravel application.
Common Contracts for Laravel
Simple Actionable for Laravel
Generate Database Schema Documentation
统计信息
- 总下载量: 3.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 12
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-03