Releases: lorisleiva/laravel-actions
v1.0.0
What's new?
⚡️ Run actions as commands (documentation)
You can now run actions as commands by simply providing the commandSignature
static property.
class PublishANewArticle extends Action
{
protected static $commandSignature = 'make:article {title} {body}';
protected static $commandDescription = 'Publishes a new article';
// ...
}
🚗 Register routes directly in actions (documentation)
You can now register your routes directly in the routes
static method of your actions.
class GetArticlesFromAuthor extends Action
{
public static function routes(Router $router)
{
$router->get('author/{author}/articles', static::class);
}
public function handle(User $author)
{
return $author->articles;
}
}
🧙♂️ instantiate an action with custom arguments (documentation)
Imagine the following GetItineraryInstructions
action that returns an array of strings given an address and a transportation mode. Here is how you would need to instantiate this action:
$action = new GetItineraryInstructions([
'address' => $address,
'mode' => 'driving',
]);
However, in this particular case, this action will always take an address
and a mode
attribute so wouldn't it be better if we could just write this instead?
$action = new GetItineraryInstructions($address, 'driving');
Well, starting from v1.0, you can. All you need to do to make this work is to define your custom logic in the getAttributesFromConstructor
method. See the documentation to learn more about this.
Note that, this will also work with the run
and dispatch
static methods.
// As an object.
GetItineraryInstructions::run($address, 'driving');
// As a job.
GetItineraryInstructions::dispatch($address, 'driving');
How to upgrade from 0.x?
📦 Composer update
# In your composer.json
"lorisleiva/laravel-actions": "^1.0",
composer update lorisleiva/laravel-actions
🚨 Potential breaking changes
-
The
register
method has been renamedinitialized
. It will be called every single time an action is instantiated no matter how it is running. (See The lifecycle of an action). -
In order to register actions as commands and register routes directly within actions, Laravel Actions need to know where your Actions are kept. By default, it will look inside the
app/Actions
directory. This new discovery mechanism might interfere with your current architecture. If this is the case, you can tell Laravel Actions were to look for actions usingActions::paths([...])
in a service provider. Alternatively you can disable this altogether via theActions::dontRegister()
method. (See Registering actions). -
Finally, A few internal method signatures have been adjusted.
- public function updateAttributeWithResolvedInstance($key, $instance) + protected function updateAttributeWithResolvedInstance($key, $instance): void - protected function findAttributeFromParameter($name, $extras = []) + protected function findAttributeFromParameter($name, $extras = []): array - protected function resolveMethodDependencies($instance, $method, $extras = []) + protected function resolveMethodDependencies($instance, $method, $extras = []): array - public function delegateTo($actionClass) + public function delegateTo(string $actionClass)
v0.3.0
⬆️ Add support for Laravel 7 (#35)
🐎 No longer instantiate every listener to check if it's an action (#33)
✨ Add the following new static helpers to run actions (#36)
MyAction::make([/* ... */])
// Equivalent to:
new MyAction([/* ... */]);
MyAction::run([/* ... */])
// Equivalent to:
(new MyAction)->run([/* ... */]);
run
method. If that's the case make sure to override the handleRun
method instead.
// Previous signature to override.
public function run(array $attributes = [])
// New signature to override.
protected function handleRun(array $attributes = [])
v0.2.5
✨ Add support for dependency injection to the following methods: rules()
, messages()
and attributes()
.
This means you can now resolve your Eloquent models or Service classes before even reaching the handle()
method.
This could be helpful, for example, when excluding a model in a unique
rule.
class UpdateTeamProfile extends Action
{
public function rules(Team $team)
{
return [
'name' => ['filled', 'max:255'],
'slug' => [
'filled',
'alpha_dash',
Rule::exists('teams')->ignore($team->id),
],
];
}
public function handle(Team $team)
{
$team->update($this->validated());
return $team;
}
}
v0.2.4
v0.2.3
v0.2.2
v0.2.1
⬆️ Add support for Laravel 6.x
v0.2.0
// Before.
public function register()
{
$this->middleware('auth');
}
// After.
public function middleware()
{
return ['auth'];
}
✨ Actions are now invokable as objects
// When running an action as an object.
$action = new PublishANewArticle([
'title' => 'My blog post',
'body' => 'Lorem ipsum.',
]);
// You can now run it like this...
$action();
// Which is equivalent to...
$action->run();