承接 cleaniquecoders/laravel-action 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cleaniquecoders/laravel-action

Composer 安装命令:

composer require cleaniquecoders/laravel-action

包简介

Simple Actionable for Laravel

README 文档

README

Latest Version on Packagist PHPStan run-tests Fix PHP code style issues Total Downloads

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 updateOrCreate behavior 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 password field will be hashed for security.
  • The ssn field 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 cleaniquecoders/laravel-action 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 12
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-03