agunbuhori/action

Laravel action package

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/agunbuhori/action

v1.0 2025-09-12 06:24 UTC

This package is auto-updated.

Last update: 2025-12-12 07:07:39 UTC


README

A lightweight, extensible base class for building Action classes in Laravel.
This package helps you keep your code clean, testable, and reusable by encapsulating business logic into Action objects with built-in validation, request handling, array-like access, and artisan generator support.

🚀 Features

  • ✅ Clean separation of business logic into Actions
  • ✅ Built-in validation via Laravel Validator
  • ✅ Automatic request binding
  • ✅ Array-like data access (ArrayAccess)
  • ✅ Combine with reusable traits like HasResponder Laravel Responder
  • Artisan command to generate new Actions quickly
  • ✅ Works smoothly with Laravel’s ecosystem

📦 Installation

composer require agunbuhori/action

🔧 Usage

1. Generate an Action via Artisan

You can scaffold a new Action using the included command:

php artisan make:action Auth/LoginAction

This will create a file at:

app/Actions/Auth/LoginAction.php

with a ready-to-use Action class template.

2. Example: LoginAction

Here’s how a real-world Action might look:

<?php

namespace App\Actions\Auth;

use Http;
use Agunbuhori\Action\Action;
use Agunbuhori\Responder\Traits\HasResponder;

class LoginAction extends Action
{
    use HasResponder;

    public function authorize(): bool
    {
        return !auth()->check();
    }

    public function rules(): array
    {
        return [
            'email'         => 'required_with:password|max:255',
            'password'      => 'required_with:email|max:255',
            'refresh_token' => 'required_without_all:email,password'
        ];
    }

    public function handle(): mixed
    {
        $data = [
            'client_id'     => config('passport.password_client_id'),
            'client_secret' => config('passport.password_client_secret'),
            'scope'         => ''
        ];

        if ($this->email && $this->password) {
            $data['grant_type'] = 'password';
            $data['username']   = $this->email;
            $data['password']   = $this->password;
        } elseif ($this->refresh_token) {
            $data['grant_type']     = 'refresh_token';
            $data['refresh_token']  = $this->refresh_token;
        }

        $response = Http::asForm()->post(url('/oauth/token'), $data);

        if ($response->failed()) {
            return $this->error(401, $response->json());
        }

        return $this->success($response->json());
    }
}

3. Validation Handling

If the input doesn’t pass validation, the response will automatically return JSON with:

{
  "status": 422,
  "errors": {
    "email": ["The email field is required when password is present."]
  },
  "message": "failed"
}

4. Run The Action

You can use an Action in two ways: directly from a route, or by calling it from a controller (or anywhere else).

Route::post('/login', LoginAction::class);

or from controller

public function login(Request $request)
{
    return LoginAction::run($request->all());
}

advanced example:

public function login(Request $request)
{
    $user = LoginAction::run($request->only('email', 'password'));

    if ($user) {
        MoveSessionCartAction::run($user);
    }

    return response()->json(['message' => 'authentication success']);
}

🛠️ Example Workflow

# Generate a new Action
php artisan make:action Users/StoreUserAction

# Edit handle() and rules() as needed
# Then run it in controllers, jobs, or anywhere in your app
$result = StoreUserAction::run($request);

❤️ License

MIT © Agun Buhori