Skip to content

v1.0.0

Compare
Choose a tag to compare
@lorisleiva lorisleiva released this 08 May 15:31
e72337b

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 renamed initialized. 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 using Actions::paths([...]) in a service provider. Alternatively you can disable this altogether via the Actions::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)