laraditz/action
Define single-purpose Action classes for Laravel and Lumen to keep code DRY. Generate actions via artisan, pass data through constructor properties, and execute with handle() or a convenient static run() method. Includes a data() helper for all properties.
Pros:
App\Actions\User, App\Actions\Order).Cons:
HandleRequests trait, actions cannot directly integrate authentication, authorization, or logging middleware.beforeHandle, afterHandle) or logging middleware, which may hinder debugging.User::find($id)), the abstraction may introduce unnecessary complexity.make:action artisan command—no additional setup required.FormRequest or Validator) unless combined with other packages.DB::transaction() to ensure data integrity.try-catch blocks or using Laravel’s ValidatesRequests).handle()).CreateUser vs. UserCreator) could lead to maintenance challenges; establish naming conventions early.composer require laraditz/action
CreateNewPost):
php artisan make:action CreateNewPost
// Before
public function store(Request $request) {
$validated = $request->validate([...]);
return Post::create($validated);
}
// After
public function store(Request $request) {
$action = new CreateNewPost(
title: $request->title,
body: $request->body
);
return $action->handle();
}
make:action command (if needed) by publishing and modifying the ActionServiceProvider.Action class to add shared behavior (e.g., validation, logging):
namespace App\Actions;
use Laraditz\Action\Action;
use Illuminate\Support\Facades\Log;
abstract class BaseAction extends Action {
public function handle(): mixed {
Log::debug("Executing action: " . static::class);
return parent::handle();
}
}
public function test_create_post_action() {
$action = new CreateNewPost(title: 'Test', body: 'Content');
$this->assertInstanceOf(Post::class, $action->handle());
}
Action class to add shared functionality (e.g., validation, event dispatching).FormRequest for validation:
public function handle(): void {
$this->validate();
Post::create($this->data());
}
public function handle(): void {
event(new PostCreated($this->data()));
Post::create($this->data());
}
public function handle(): void {
DB::transaction(function () {
Post::create($this->data());
// Additional DB operations
});
}
How can I help you explore Laravel packages today?