Member-only story
Laravel — P26: 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.