hachicode/action-pattern

This package implements actions for Laravel 11 based in Command pattern for execute functions in the granular way

Installs: 567

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/hachicode/action-pattern

1.0.2 2025-06-07 17:30 UTC

This package is auto-updated.

Last update: 2025-12-07 19:06:01 UTC


README

This package implements a basic action pattern, based in command pattern using the Validator by default from laravel

Features

  1. Command for create action
  2. Config file for custom directory based in namespace configured and psr-4 standard

Instructions

composer require hachicode/action-pattern
php artisan vendor:publish --tag=action-pattern-config

Commands

Generate command without validation

php artisan make:action LoginAction

Generate command with validation

php artisan make:action LoginValidatedAction --validated

Structure the classes generated

Action simple
namespace App\Actions;

use Hachicode\ActionPattern\Classes\Action;

class LoginAction extends Action
{
    /**
     * Execute the action
     *
     * @return mixed
     */
    public function handle(array $data) : mixed
    {
        // Set your logic
        return $data;
    }
}
Action validated
namespace App\Actions;

use Illuminate\Support\Facades\Validator;
use Hachicode\ActionPattern\Classes\ActionValidated;

class LoginValidatedAction extends ActionValidated
{
    /**
    * @param array $data Is the validated result from method validation
    */
    public function handle(array $data): mixed
    {
        // Execute your logic with your data validated
        return $data;
    }

    public function validation(array $data): array
    {
        $validator = Validator::make($data, [
            // Set your rules
        ]);

        if ($validator->fails()) {
            throw new \Exception(message: 'Validation failed in LoginValidatedAction action' . 
            collect($validator->errors())->join(', '));
        }

        return $validator->validated();
    }
}

Execute the action

use App\Actions\LoginValidatedAction;

// ...

$result = LoginValidatedAction::run($data);
...