Skip to main content

Beyond the Controller: Reusing Logic Everywhere

Premium
4 min read

Throughout this course, we've been extracting logic from the Controller into Services, Actions, Jobs, and Events. But why did we do all that? Just to make the Controller shorter?

No. The real payoff is reusability. Once your business logic lives in a Service or Action class, it can be called from anywhere — not just Controllers.

Let's demonstrate this with a real example: the same CreateUserAction we built earlier, reused across four different entry points.


1. From a Controller (Web or API)

This is what we've been doing throughout the course:

class UserController extends Controller
{
public function store(StoreUserRequest $request, CreateUserAction $action)
{
$user = $action->execute($request->validated());
 
return response()->json(['data' => $user], 201);
}
}

Nothing new here. But now let's look at the same Action used elsewhere.


2. From an Artisan Command

Imagine you need to create users via the command line — for seeding, for admin scripts, or for importing from a CSV file.

php artisan make:command CreateUserCommand

app/Console/Commands/CreateUserCommand.php:

use App\Actions\CreateUserAction;
 
class CreateUserCommand extends Command
{
protected $signature = 'users:create {name} {email} {password}';
protected $description = 'Create a new user from the command line';
 
public function handle(CreateUserAction $action)
{
$user = $action->execute([
'name' => $this->argument('name'),
'email' => $this->argument('email'),
'password' => bcrypt($this->argument('password')),
'roles' => [],
]);
 
$this->info("User {$user->name} created successfully.");
}
}

The Action doesn't care who called it. It just receives data and creates

The Full Lesson is Only for Premium Members

Want to access all of our courses? (34 h 11 min)

You also get:

58 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.