Download the PHP package shafimsp/laravel-actions without Composer
On this page you can find all versions of the php package shafimsp/laravel-actions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shafimsp/laravel-actions
More information about shafimsp/laravel-actions
Files in shafimsp/laravel-actions
Package laravel-actions
Short Description An action executor with middleware pipeline for Laravel applications.
License MIT
Informations about the package laravel-actions
Laravel Actions
A lightweight action executor with middleware pipeline for Laravel applications. Implements the CQRS pattern with convention-based handler resolution, return type validation, caching, and first-class testing support.
Requirements
- PHP 8.2+
- Laravel 11.x or 12.x
Installation
The package auto-registers its service provider. Optionally publish the config:
Quick Start
1. Define an Action
Actions are simple classes that implement the Action interface and declare their return type via a docblock:
2. Create a Handler
By convention, the handler class is the action class name suffixed with Handler in the same namespace:
3. Execute the Action
Defining Actions
Declare the return type using the @implements docblock:
Supported return types: scalars (string, int, float, bool, array), objects, nullable types, union types, void, and mixed.
The executor validates the handler's return value against the declared type at runtime and throws a RuntimeException on mismatch.
Handler Resolution
Convention-Based (Default)
Append Handler to the action class name in the same namespace:
| Action | Handler |
|---|---|
App\Actions\CreateUser |
App\Actions\CreateUserHandler |
App\Queries\FindUser |
App\Queries\FindUserHandler |
Attribute-Based
Use the #[HandledBy] attribute to specify a custom handler:
Middleware
Middleware wraps the action execution pipeline, enabling cross-cutting concerns like logging, authorization, or caching.
Class-Based Middleware
Closure-Based Middleware
Global Middleware
Register middleware for every action in config/actions.php:
Middleware can short-circuit the pipeline by returning early without calling $next(), or modify the action before passing it along.
Caching
Cache action results automatically using the #[Cacheable] attribute:
Parameters:
ttl— Time to live in seconds (default:3600)key— Custom cache key (optional, auto-generated from action class and properties if omitted)
The CacheMiddleware is included in the default middleware stack. It uses cache tags when available and a distributed lock to prevent cache stampede.
Bootstrap Cache
For production, generate a bootstrap cache to skip reflection on every request:
This discovers all actions, resolves their handlers and return types, and writes a cache file to bootstrap/cache/actions.php.
To clear:
Configuration
Testing
The package provides a FakeExecutor with a fluent assertion API.
Faking the Executor
Assertions
Full Test Example
License
MIT
All versions of laravel-actions with dependencies
illuminate/container Version ^11.0|^12.0
illuminate/pipeline Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0
spatie/laravel-package-tools Version ^1.6