Action classes pattern:
Create Action
class CreateUserAction
{
public function execute(array $data): User
{
$user = User::create($data);
event(new UserCreated($user));
return $user;
}
}Use in Controller
public function store(Request $request, CreateUserAction $action)
{
$user = $action->execute($request->validated());
return redirect()->route('users.show', $user);
}With Dependencies
class ProcessOrderAction
{
public function __construct(
private PaymentGateway $payment,
private NotificationService $notifications
) {}
public function execute(Order $order): void
{
$this->payment->charge($order);
$this->notifications->orderConfirmed($order);
}
}