uteq/laravel-model-actions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

uteq/laravel-model-actions

Composer 安装命令:

composer require uteq/laravel-model-actions

包简介

Magically adds actions to a model

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package will magically add actions to a model. Simply adding the WithActions trait:

Add the trait to your model

use Uteq\ModelActions\Concerns\WithActions;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use WithActions;
}

And than use it in all sorts of ways:

$user->action()->update($input);
$user->action('update', $input);
$user->action(\App\Actions\User\Update::class, $input);

Whenever you need to call a method on a model that does not (yet) have an active record in the database, you can also use this static method.

User::do(Create::class, $input);

This package was inspired by this read about OOP: https://www.tonysm.com/when-objects-are-not-enough/#objects-in-the-large Especially the last part about actions being added to a model made sense to me. This will keep your models clean, and your actions separated.

The idea behind this package is that adding actions makes your application more scalable. 'Grouping' these actions by adding them to a model, makes it easier to comprehend where the action is for. It is more declarative having your model $user->action() perform an action than having the action out of the blue. To create more context you would have to always prefix your action UserCreateAction for example.

Not convinced about using Actions in your application? Read this excellent blog post of Brent from Spatie.be https://stitcher.io/blog/laravel-beyond-crud-03-actions

You will need php 8.0

Installation

You can install the package via composer:

composer require uteq/laravel-model-actions

You can publish the config file with:

php artisan vendor:publish --provider="Uteq\ModelActions\ModelActionsServiceProvider" --tag="laravel-model-actions-config"

This is the contents of the published config file:

return [
    /**
     * You can overwrite the method used to handle the
     * action. By default this is __invoke.
     */
    'method' => '__invoke',    
];

Usage

On your model

Add the WithActions trait to the model

use Uteq\ModelActions\Concerns\WithActions;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use WithActions;
}

Directory

The actions should be added in the following folder structure

App
├── Actions
│   └── User
│       ├── Create.php 
│       ├── Update.php 
│       ├── Destroy.php
│       └── AddImage.php
└── Models
    └── User.php

After that you can always access the actions from your model:

This is how an action class looks like:

class Update
{
    public function __invoke(User $model, array $input = [])
    {
        // Now add you own logic here
    }
}

As you can see the $user will automatically be injected into the __invoke method. The system knows the user because you are calling in from the user. Please note that the model is always the first parameter.

The name of the Action class will be used as the method name. So a class UpdateImage will be accessible using User::action()->updateImage($input);

$user->action(Update::class, $input);

Dependency injection in Actions

Dependency injection in the __construct of the action is by default. So you can do this:

class Destroy
{
    public function __construct(
        protected PublicDestroyer $destroyer,
    ) { }
    
    public __invoke(User $user, array $input = [])
    {
        ($this->destroyer)($user, $input);
    }
}

Parameter binding

Parameter binding for model actions is pretty straight forward. We have Named parameters (see below) and simply using the given order:

class Action
{
    public function __invoke($var1, $var2)
    {
    
    }
}
User::do(Action::class, 'var1', 'var2');

Named parameters

Utilizing php 8's named paramters you are able to be very strict into what your action class accepts.

class Action
{
    public function __invoke($name)
    {
    
    }
}
User::do(Action::class, name: 'test');

Examples

Apply with actions to all your models

A convenient way to add the WithActions to all your Models is by simply extending the Eloquent Model class and extend upon that class.

namespace App\Support;

use Uteq\ModelActions\Concerns\WithActions;

class Model extends \Illuminate\Database\Eloquent\Model
{
    use WithActions;
}
class ActionModel extends \App\Support\Model
{

} 

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

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.

uteq/laravel-model-actions 适用场景与选型建议

uteq/laravel-model-actions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 379 次下载、GitHub Stars 达 10, 最近一次更新时间为 2021 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「uteq」 「laravel-model-actions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 uteq/laravel-model-actions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 uteq/laravel-model-actions 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 379
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-09