Refactoring to Action Classes in Laravel

Mohasin Hossain
2 min readOct 29, 2024

--

As your Laravel project grows, organizing complex business logic becomes essential to maintain clean, manageable code. Refactoring to actions is one powerful approach that can help.

What are Action Classes?

Action classes are dedicated PHP classes that encapsulate a specific piece of logic, which can be reused in various places, such as controllers, commands, and even queues. They help remove bloat from controllers and models, making your code more organized and easier to test.

Benefits of Refactoring to Actions

  1. Separation of Concerns: By moving logic into dedicated classes, you achieve clearer separation between different responsibilities. For example, rather than having a user registration process scattered across a controller, you can create a RegisterUserAction class that handles the entire flow.
  2. Reusability: Logic that lives inside an action class can easily be reused across the application. Whether you are calling it from a controller, a command, or a job, the action can be used without duplicating code.
  3. Testability: Testing an action class becomes straightforward since the logic is isolated. You can unit test the action without having to mock the request, controller, or any other dependencies.
  4. Maintainability: When you refactor code into actions, your controllers and models become much more manageable. Each action does one thing well, allowing for easier updates, refactoring, and feature extensions later.

Implementing Action Classes in Laravel

To implement an action class, follow these steps:

Create an Action Class: Laravel doesn’t include a default generator for actions, but you can easily create a class using artisan:

php artisan make:class RegisterUserAction

Define the Action Logic: In the class, define a method to handle the business logic, commonly named execute. For example:

class RegisterUserAction
{
public function execute(array $data)
{
$user = User::create($data);
// Send welcome email, trigger events, etc.
return $user;
}
}

Use the Action: You can now use this action in your controller:

class UserController extends Controller
{
public function store(Request $request, RegisterUserAction $registerUser)
{
$user = $registerUser->execute($request->all());
return response()->json($user);
}
}

Making Actions Queueable

One of the powerful aspects of action classes is their ability to be queued for asynchronous execution. This is useful for tasks like sending emails, processing orders, or generating reports.

You can implement this by making the class implement the ShouldQueue interface and using Laravel’s queuing features:

class RegisterUserAction implements ShouldQueue
{
public function execute(array $data)
{
$user = User::create($data);
// Async logic, such as sending email notifications
return $user;
}
}

Conclusion

By refactoring your business logic into actions, you create a more maintainable, reusable, and testable codebase. Whether you’re handling user registration, processing orders, or triggering notifications, action classes provide a clean structure that enhances both the clarity and efficiency of your code.

Book a session with me: https://adplist.org/mentors/mohasin-hossain

--

--

Mohasin Hossain
Mohasin Hossain

Written by Mohasin Hossain

Senior Software Engineer | Mentor @ADPList | Backend focused | PHP, JavaScript, Laravel, Vue.js, Nuxt.js, MySQL, TDD, CI/CD, Docker, Linux

No responses yet