lorisleiva/laravel-actions
Laravel Actions organizes your app around single-purpose “action” classes. Write the core logic once in handle(), then run it as a controller, job, listener, command, or plain object. Keep business logic reusable, testable, and consistent across entry points.
PublishANewArticle::run()) and reusability across multiple contexts (e.g., CLI, API, events).php artisan make:action generates a scaffold with asController, asJob, etc.).asListener), and testing frameworks (e.g., PestPHP’s make:action stubs).assertActionRuns()). Mitigation: Leverage the package’s built-in test helpers.auth, throttle)?assertActionRuns()), or build custom assertions?make:action command over make:controller?JobDecorator) for project-specific needs?actingAs).asListener for event-driven workflows).| Phase | Steps | Risks | Mitigation |
|---|---|---|---|
| Assessment | Audit 10% of controllers for action suitability (e.g., complex logic). | Underestimation of effort. | Start with non-critical modules. |
| Pilot | Replace 1–2 controllers with actions in a sandbox environment. | Team resistance to change. | Demonstrate reduced boilerplate. |
| Incremental | Migrate modules by feature (e.g., "Payments" → "Invoices"). | Broken dependencies. | Use feature flags for gradual rollout. |
| Full Adoption | Replace all controllers/jobs with actions. | Testing debt. | Automate test generation for actions. |
asController (e.g., return new ArticleResource($article)).FormRequest via validate() helper in actions.JobDecorator) for project-specific needs.make:action stubs to enforce team conventions (e.g., naming, docblocks).composer.json and run composer require lorisleiva/laravel-actions.php artisan vendor:publish --tag="actions-config".make:action vs. make:controller.asJob + asListener).handle() updates all contexts (HTTP, queue, event).LoggableActionDecorator).$this->context() to inspect runtime data (e.g., request, event).handle() in custom decorators to add telemetry.handle() (e.g., throw new ActionFailed($e)).jobFailed(Throwable $e) for async actions to notify support systems.asController, asJob, etc.asJob for long-running tasks (e.g., PDF generation).PublishANewArticle::run()).shouldQueue() for event-driven scaling.action:executed Laravel event).ActionFailed events).| Failure Type | Impact | Mitigation |
|---|---|---|
| Action Logic Error | Breaks all contexts (HTTP, queue). | Unit test handle() with edge cases. |
| Decorator Bug | Fails silently (e.g., asJob). |
Custom decorators with validation. |
| Dependency Failure | External API call in handle(). |
Retry logic (e.g., spatie/laravel-retryable). |
| Testing Gaps | Untested action paths. | Mandate test coverage for new actions. |
| Version Mismatch | Package/Laravel compatibility. | Pin versions in composer.json. |
How can I help you explore Laravel packages today?