Single Action Controller in Laravel

In this post, we’re going to find out what is a Single Action Controller, how to create one, and what are the pros and cons of using it.

Let’s dive in!

 

 

What is a Single Action Controller?

Let’s take this UserController as an example.

Let’s suppose that this Controller can have only one action, that of listing the names and emails of all the users from the database.

And that’s exactly what a Single Action Controller is.

It is a Controller dedicated to a single action.

It can be create, update, delete, list, show, or whatever action your business needs.

Now you may ask:

Well, is that it? Do you just create a Controller with one method?

Well…yeah…kind of.

You can think of this Controller as a Single Action Controller, but there’s a better approach to writing this.

Let’s find out!

How to create a Single Action Controller

Instead of having a named method, in our case list, we can leverage the power of PHP’s __invoke() magic method.

The __invoke() magic method is called when an object is called as a function.

Meaning, that when our Controller is called, the __invoke magic method is also triggered.

We should also change the name of the Controller to be aligned with its purpose.

So instead of UserController let’s call it ListUserController.

Next, let’s also edit the route of the Controller because there is no need to specify a method as the action is assumed to be the __invoke() method.

Before we move on, I’d also like to point out that there is an artisan command which you can use to easily generate a Single Action Controller.

php artisan make:controller UserController --invokable

Now, let’s go ahead and talk about the pros and cons of a Single Action Controller.

Pros

As you may have noticed, a Single Action Controller has only one responsibility.

In our case, it is responsible for listing all the users from the database.

In a less complex application, this can be a huge advantage because your Controller:

✅ has one job only

✅ has a descriptive name and it’s easy to understand what it does

✅ is easy to change

Moreover, I think a Single Action Controller really shines when it comes to serving static pages such as Terms and Conditions, About, Contact, and other similar pages.

Finally, let’s see what the cons are.

❌ Cons

I think that the Single Action Controller is not well suited for RESTFful applications ❌

What do I mean by this?

In a RESTful approach, a Controller should be responsible for taking care of certain actions for an Entity.

Let’s take the User Entity for example.

A UserController should be responsible for CRUD operations for the User Entity.

But in a more complex app, you’d also have operations for listing, showing, filtering, and so on, for this Entity.

I think it would be a bad idea to have a Single Action Controller for each operation because you’d end up with several Controllers for each Entity.

So in our case, the User Entity, we’d have CreateUserController, ReadUserController, UpdateUserController, DeleteUserController, ListUserController and you get my point.

Moreover, if our business logic consists of multiple Entities, which is usually the case, you’ll end up having tons of Controllers in your codebase.

I think this approach is not a good idea if you’re aiming for scalability in terms of development ❌


Alright, now it’s your time to let me know what is your take on Single Action Controllers.

Do you use them?

When do you think is best to use them?

Let me know in the comments!

I hope you’re more familiar now with Single Action Controllers and you can put them to good use in your existing or new applications.

 

 

Let me know what you think about this article through comments below, or on Twitter at @pelu_carol.

If you find this article helpful, please share it with others and subscribe to the blog to support me, and receive a bi-monthly-ish e-mail notification on my latest articles.

 

You may also like...