Skip to main content

Overview

Actions are reusable classes that encapsulate business logic. They follow the action pattern and can be invoked from controllers, commands, or other parts of your application.

Creating an Action

Actions are typically located in the src/Actions directory. Here’s a basic structure:
namespace Esign\UnleashCommerce\Core\Actions;

class CreateProductAction
{
    public function execute(array $data)
    {
        // Your business logic here
    }
}

Using Actions

Actions can be injected into your controllers or other classes:
public function store(CreateProductAction $createProduct)
{
    $product = $createProduct->execute($validated);
    return response()->json($product);
}

Best Practices

  • Single Responsibility: Each action should do one thing well
  • Dependency Injection: Use Laravel’s container for dependencies
  • Easy Testing: Actions are straightforward to unit test
  • Reusability: Use the same action from multiple places in your code

Action Types

Common action patterns in Unleash Commerce include:
  • Create actions
  • Update actions
  • Delete actions
  • Custom business logic actions