PHP code example of hachicode / action-pattern

1. Go to this page and download the library: Download hachicode/action-pattern library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hachicode / action-pattern example snippets


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;
    }
}


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();
    }
}

bash 
php artisan make:action LoginAction
bash 
php artisan make:action LoginValidatedAction --validated