定制 panchodp/laravel-actions 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

panchodp/laravel-actions

最新稳定版本:v2.4.6

Composer 安装命令:

composer require --dev panchodp/laravel-actions

包简介

Make your Laravel actions classes fast and in a simple way.

README 文档

README

Logo for Laravel Action

Php Total Downloads Latest Stable Version License Tests

Laravel Actions

Make your Laravel actions classes fast and in a simple way.

Compatibility

Laravel PHP Package
11.x 8.3+ ^2.x
12.x 8.3+ ^2.x
13.x 8.3+ ^2.x

Installation

You can install the package via composer:

composer require panchodp/laravel-actions --dev

Configuration

Publish the configuration file:

php artisan vendor:publish --provider="Panchodp\LaravelAction\LaravelActionServiceProvider" --tag="laravel-actions-config"

This creates config/laravel-actions.php:

return [
    'base_folder' => 'Actions',
    'method_name' => 'handle',
];
  • base_folder: Base folder where action classes are created. Defaults to Actions (app/Actions).
  • method_name: Method name generated in action classes. Defaults to handle.

Customizing Stubs

You can publish and edit the stub templates used to generate action classes:

php artisan vendor:publish --provider="Panchodp\LaravelAction\LaravelActionServiceProvider" --tag="laravel-actions-stubs"

This publishes the 4 stubs to resources/stubs/vendor/laravel-actions/. Once published, the package will use your custom stubs instead of the defaults. You can customize each stub independently — any stub not found in your published directory will fall back to the package default.

Method Types

By default, Laravel Actions generates instance methods for better flexibility and dependency injection support. However, you can create static methods when needed for simpler usage.

Instance Methods (Default)

// Usage
$action = new MyAction();
$action->handle($attributes);

// Generated code
public function handle(array $attributes): void
{
    // Implementation
}

Static Methods

// Usage
MyAction::handle($attributes);

// Generated code
public static function handle(array $attributes): void
{
    // Implementation
}

Usage

Interactive Mode

Run make:action without arguments to launch an interactive wizard:

php artisan make:action
 ┌ Action name ───────────────────────────────────────────────┐
 │ e.g. CreateUser                                            │
 └────────────────────────────────────────────────────────────┘
 ┌ Subfolder (optional) ──────────────────────────────────────┐
 │ e.g. User or User/Auth                                     │
 └────────────────────────────────────────────────────────────┘
 ┌ Include DB transaction? ───────────────────────────────────┐
 │ ● Yes / ○ No                                               │
 └────────────────────────────────────────────────────────────┘
 ...

Creating Actions

To create an action class, use the make:action command. You can specify the full path using forward slashes / or backslashes \ (Laravel-style syntax):

Basic action:

php artisan make:action MyAction

This creates a new action class in the app/Actions directory.

Action in a subfolder:

php artisan make:action User/CreateAccount

This creates the action in app/Actions/User/CreateAccount.php.

Action in nested subfolders:

php artisan make:action User/Auth/Login

This creates the action in app/Actions/User/Auth/Login.php.

Using backslashes (alternative syntax):

php artisan make:action Admin\DeletePost

This creates the action in app/Actions/Admin/DeletePost.php.

The generated class will have a handle method where you can implement your action logic:

<?php

declare(strict_types=1);

namespace App\Actions\User;

use Throwable;

final class CreateAccount
{
    public function handle(array $attributes): void
    {
        // This is where the action logic will be implemented.
    }
}

Flags

Flag Shortcut Description
--transaction -t Wraps the action body in a DB::transaction
--user -u Injects User $user into the method
--request -r Generates a Request class and injects it into the method
--static -s Generates a static method instead of an instance method
--force Overwrites the action if it already exists

Shortcuts can be combined in any order, just like make:model:

php artisan make:action MyAction -tur    # transaction + user + request
php artisan make:action MyAction -turs   # + static method

# Long form works too
php artisan make:action MyAction --transaction --user --request

Example output for --turs:

final class MyAction
{
    public static function handle(User $user, MyActionRequest $request): void
    {
        DB::transaction(function () use ($request) {
            // Logic to be executed within the transaction
        });
    }
}

Other Userfull Commands:

You can show the Actions directory tree in the terminal with the following command:

php artisan actions:list

This will display the structure of the app/Actions or the base directory specified in the config file, showing all action classes and their subdirectories.

Actions/
├── Folder1/
│   ├── SecondAction
│   └── ThirdAction
├── Folder2/
│   └── FourthAction
├── FirstAction
└── LastAction

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

统计信息

  • 总下载量: 274
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固