panchodp / laravel-actions
Make your Laravel actions classes fast and in a simple way.
Requires
- php: ^8.3.0 | ^8.4.0
Requires (Dev)
- larastan/larastan: ^3.5.0
- laravel/pint: ^1.23
- orchestra/testbench: ^10.4
- pestphp/pest: ^3.8.2
- spatie/ray: ^1.42
README
Laravel Actions
Make your Laravel actions classes fast and in a simple way.
Installation
You can install the package via composer:
composer require panchodp/laravel-actions --dev
Configuration
Use the following command to publish the configuration file:
php artisan vendor:publish --provider="Panchodp\LaravelAction\LaravelActionServiceProvider" --tag="laravel-actions-config"
This will create a config/laravel-actions.php
file in your application.
return [ 'base_folder' => 'Actions', 'method_name' => 'handle', 'directory_permissions' => 0750, 'method_static' => true, ];
There are four configuration options available:
base_folder
: This is the base folder where your action classes will be created. By default, it is set toActions
, which means your action classes will be created in theapp/Actions
directory.method_name
: This is the name of the method that will be created in your action classes. By default, it is set tohandle
, which means your action classes will have ahandle
method where you can implement your action logic.directory_permissions
: This option defines the permissions for newly created actions folders. This option defines the permissions for newly directories created by the package.method_static
: Controls whether action methods are static by default. When set totrue
(default), methods are generated as static. When set tofalse
, methods are generated as instance methods. This can be overridden using the--i
flag.
Method Types
By default, Laravel Actions generates static methods for better performance and simpler usage. However, you can create instance methods when needed for dependency injection or other use cases.
Static Methods (Default)
// Usage MyAction::handle($attributes); // Generated code public static function handle(array $attributes): void { // Implementation }
Instance Methods
// Usage $action = new MyAction(); $action->handle($attributes); // Generated code public function handle(array $attributes): void { // Implementation }
Usage
- To make an action class, you can use the
make:action
command:
php artisan make:action MyAction
This will create a new action class in the app/Actions
directory with the name MyAction.php
.
The class will have a handle
method where you can implement your action logic.
<?php declare(strict_types=1); namespace App\Actions; use Throwable; final class MyAction { public static function handle(array $attributes): void { // This is where the action logic will be implemented. } }
- To make an action class in a specific subfolder of Action, you can use:
php artisan make:action MyAction Folder
This will create a new action class in the app/Actions/Folder
directory with the name MyAction.php
.
<?php declare(strict_types=1); namespace App\Actions\Folder; use Throwable; final class MyAction { public static function handle(array $attributes): void { // This is where the action logic will be implemented. } }
Or you can use more than one subfolder like this:.
php artisan make:action MyAction Folder1/Folder2
This will create a new action class in the app/Actions/Folder1/Folder2/
directory with the name MyAction.php
.
Flags
--t
This flag prepare the action class with Database trasactions. For example, if you want to create an action class with transactions, you can use:
php artisan make:action MyAction --t
will result in the following action class:
<?php declare(strict_types=1); namespace App\Actions; use Illuminate\Support\Facades\DB; use Throwable; final class MyAction { public static function handle(array $attributes): void { DB::transaction(function () use ($attributes) { // Logic to be executed within the transaction }); } }
--u
This flag inyect User $user in the handle method.
For example, if you want to create an action class with User injection, you can use:
php artisan make:action MyAction --u
will result in the following action class:
<?php declare(strict_types=1); namespace App\Actions; use App\Models\User; use Throwable; final class MyAction { public static function handle(User $user,array $attributes): void { // This is where the action logic will be implemented. } }
--tu
or--ut
Use both flags together to prepare the action class with Database transactions and User injection.
bash php artisan make:action MyAction --tu
or bash php artisan make:action MyAction --ut
will result in the following action class:
<?php declare(strict_types=1); namespace App\Actions; use Illuminate\Support\Facades\DB; use App\Models\User; use Throwable; final class MyAction { public static function handle(User $user,array $attributes): void { DB::transaction(function () use ($attributes) { // Logic to be executed within the transaction }); } }
--r
This flag generates a Laravel Request class and injects it into the action method.
For example, if you want to create an action class with Request injection, you can use:
php artisan make:action MyAction --r
This will generate both an Action class and a Request class (MyActionRequest
), and will result in:
<?php declare(strict_types=1); namespace App\Actions; use App\Http\Requests\MyActionRequest; final class MyAction { public static function handle(MyActionRequest $request): void { // This is where the action logic will be implemented. } }
--i
This flag generates instance methods instead of static methods.
For example, if you want to create an action class with instance methods, you can use:
php artisan make:action MyAction --i
will result in the following action class:
<?php declare(strict_types=1); namespace App\Actions; final class MyAction { public function handle(array $attributes): void { // This is where the action logic will be implemented. } }
Advanced Flag Combinations
You can combine multiple flags to create actions with different features. All possible combinations are supported:
Two-flag combinations:
--tr
or--rt
: Database transactions with Request injection--ur
or--ru
: User injection with Request injection--tu
or--ut
: Database transactions with User injection (as shown above)--ti
or--it
: Database transactions with instance method--ui
or--iu
: User injection with instance method--ri
or--ir
: Request injection with instance method
Three-flag combinations:
--tur
,--tru
,--utr
,--urt
,--rtu
,--rut
: All features combined (static)--tui
,--tiu
,--uti
,--uit
,--itu
,--iut
: Transactions + User + Instance--tri
,--tir
,--rti
,--rit
,--itr
,--irt
: Transactions + Request + Instance--uri
,--uir
,--rui
,--riu
,--iru
,--iur
: User + Request + Instance
Four-flag combinations (all features):
--turi
,--triu
,--utri
,--urti
,--rtui
,--ruti
: All features with instance method--itru
,--itur
,--iutr
,--iurt
,--irtu
,--irut
: All permutations supported
For example with static method (default):
php artisan make:action CompleteAction --tur
This will generate:
<?php declare(strict_types=1); namespace App\Actions; use Illuminate\Support\Facades\DB; use App\Models\User; use App\Http\Requests\CompleteActionRequest; final class CompleteAction { public static function handle(User $user, CompleteActionRequest $request): void { DB::transaction(function () use ($request) { // Logic to be executed within the transaction }); } }
For example with instance method:
php artisan make:action CompleteAction --turi
This will generate an instance method with all features:
<?php declare(strict_types=1); namespace App\Actions; use Illuminate\Support\Facades\DB; use App\Models\User; use App\Http\Requests\CompleteActionRequest; final class CompleteAction { public function handle(User $user, CompleteActionRequest $request): void { DB::transaction(function () use ($request) { // Logic to be executed within the transaction }); } }
Individual flags are also supported:
--t
: Only database transactions--u
: Only user injection--r
: Only request injection--i
: Only instance method (non-static)
Configuration vs Flags
You can control the default behavior through configuration and override it with flags:
// config/laravel-actions.php 'method_static' => false, // Makes instance methods default
# With method_static = false php artisan make:action MyAction # Creates instance method php artisan make:action MyAction --i # Still creates instance method (redundant) # With method_static = true (default) php artisan make:action MyAction # Creates static method php artisan make:action MyAction --i # Creates instance method (override)
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.