Laravel — P26: Single Action Controllers

Simplifying Laravel: The Power of Single Action Controllers

Controllers are there to make our lives simpler, not to make them more complex. However, even when we separate the logic out of our route files, sometimes the controllers themselves can also become cluttered. That’s one use case for a Single Action Controller.

The Single Action Controller will contain a specifically named method: __invoke(). Naming the method __invoke() will eliminate the need to call it from our route. Instead, we can just call the Controller itself.

Creating the Single Action Controller

Creating the Single Action Controller is no different than creating a regular controller. In your terminal, type in the following:

php artisan make:controller SacTestController

That generates a new controller skeleton for us.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SacTestController extends Controller
{
    //
}
Within our controller, we need to add the __invoke() method.
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SacTestController extends Controller
{
    public function __invoke()
    {
        return "I'm being invoked. I know...original.";
    }
}

And that does it for our Single Action Controller. Nothing more complex than that. Now we just need to create a route to call it.

Single Action Controller Route

The only difference that you’ll see is that the method itself is not going to be called. Since there is no method call, we do not pass an array like we did previously. For example, when we created our show method, we had the following route:

<?php
use App\Http\Controllers\TestController;

Route::get('/test-controller/show', [TestController::class, 'show']);

With the single action controller, the array goes away and so does the method.

<?php
use App\Http\Controllers\SacTestController;

Route::get('/test-single-action-controller', SacTestController::class);

Passing an Argument from the URL to the Single Action Controller

Again, an almost identical process. All we need to do is specify the argument that we want to pass and add it to our __invoke() method.

<?php
use App\Http\Controllers\SacTestController;

Route::get('/test-single-action-controller/{name}', SacTestController::class);
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SacTestController extends Controller
{
    public function __invoke($name)
    {
        return "I'm being invoked. I know...original: " . $name;
    }
}

We can see it in action by visiting: http://0.0.0.0/test-single-action-controller/Dino. The response that we get is: I’m being invoked. I know…original: Dino.

Single Action Controllers are simple and can be used when there’s just too much logic in your standard controllers. Use it if you need it.

Laravel Series

Continue your Laravel Learning.

Laravel — P25: Passing URL Arguments to Controllers

Dynamic Data Handling: URL Arguments in Laravel Controllers

Laravel – P25: URL Arguments To Controllers

Learn to pass URL arguments to Laravel controllers for dynamic content delivery. Enhance user interaction with tailored responses.

Laravel — P26: Single Action Controllers

Simplifying Laravel: The Power of Single Action Controllers

Laravel – P26: Single Action Controllers

Optimize your Laravel application with single action controllers for focused functionality. Streamline tasks for efficiency & clarity.

Laravel — P27: Database Intro

Laravel Database Mastery: Starting Your Data Journey

Laravel – P27: Database Intro

Kickstart your Laravel project with a database intro. Unlock the secrets to managing data effectively for powerful, dynamic applications.

Leave a Reply