lorisleiva / laravel-actions
Laravel components that take care of one specific task
Fund package maintenance!
lorisleiva
Installs: 3 906 109
Dependents: 66
Suggesters: 1
Security: 0
Stars: 2 511
Watchers: 32
Forks: 123
Open Issues: 11
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- lorisleiva/lody: ^0.5
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^1.23|^2.34
- phpunit/phpunit: ^9.6|^10.0
- dev-main
- v2.8.4
- v2.8.3
- v2.8.2
- v2.8.1
- v2.8.0
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.6.0
- v2.5.1
- v2.5.0
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.0
- v2.3.0-beta.2
- v2.3.0-beta
- v2.2.0
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 1.x-dev
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.3.0
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-loris/support-php82
- dev-loris/drop-l8
- dev-loris/fix-ci
- dev-loris/fix-207
- dev-loris/fix-199-again
- dev-add-tests-for-windows
- dev-fix-157
- dev-fix-142
- dev-feature/laravel-9-upgrade
- dev-feature/support-controllers-with-explicit-methods
- dev-feature/with-attributes
This package is auto-updated.
Last update: 2024-11-10 10:31:24 UTC
README
⚡ Classes that take care of one specific task.
This package introduces a new way of organising the logic of your Laravel applications by focusing on the actions your applications provide.
Instead of creating controllers, jobs, listeners and so on, it allows you to create a PHP class that handles a specific task and run that class as anything you want.
Therefore it encourages you to switch your focus from:
"What controllers do I need?", "should I make a FormRequest for this?", "should this run asynchronously in a job instead?", etc.
to:
"What does my application actually do?"
Installation
composer require lorisleiva/laravel-actions
Documentation
📚 Read the full documentation at laravelactions.com
Basic usage
Create your first action using php artisan make:action PublishANewArticle
and define the asX
methods when you want your action to be running as X
. E.g. asController
, asJob
, asListener
and/or asCommand
.
class PublishANewArticle { use AsAction; public function handle(User $author, string $title, string $body): Article { return $author->articles()->create([ 'title' => $title, 'body' => $body, ]); } public function asController(Request $request): ArticleResource { $article = $this->handle( $request->user(), $request->get('title'), $request->get('body'), ); return new ArticleResource($article); } public function asListener(NewProductReleased $event): void { $this->handle( $event->product->manager, $event->product->name . ' Released!', $event->product->description, ); } }
As an object
Now, you can run your action as an object by using the run
method like so:
PublishANewArticle::run($author, 'My title', 'My content');
As a controller
Simply register your action as an invokable controller in a routes file.
Route::post('articles', PublishANewArticle::class)->middleware('auth');
As a listener
Simply register your action as a listener of the NewProductReleased
event.
Event::listen(NewProductReleased::class, PublishANewArticle::class);
Then, the asListener
method of your action will be called whenever the NewProductReleased
event is dispatched.
event(new NewProductReleased($manager, 'Product title', 'Product description'));
And more...
On top of running your actions as objects, controllers and listeners, Laravel Actions also supports jobs, commands and even mocking your actions in tests.
📚 Check out the full documentation to learn everything that Laravel Actions has to offer.