Invokable Controller for Laravel 8

<?php

/********** Invokable Controller **********/
namespace App\Http\Controllers;

class InvokableController extends Controller
{
    public function __invoke()
    {
        return 'This is an invokable controller';
    }
}

?>

<?php

/********** routes/web.php **********/
use App\Http\Controllers\InvokableController;

Route::get('/invokable', InvokableController::class);

?>
namespace App\Http\Controllers;

class InvokableController extends Controller
{
    public function __invoke()
    {
        return 'This is an invokable controller';
    }
}
use App\Http\Controllers\InvokableController;

Route::get('/invokable', InvokableController::class);

Laravel 8 gets rid of the automatic namespace attached to routes, so going forward you will have to use the FQCN by default, it can always be added back in you if you wanna use the old routing style, just modify your RouteServiceProvider.

public function boot()
{
    $this->routes(function () {
        Route::middleware('web')
            ->namespace('App\Http\Controllers')
            ->group(base_path('routes/web.php'));

}
BETA Snippet explanation automatically generated by OpenAI:

Here is what the above code is doing:
1. Imports the InvokableController class from the AppHttpControllers namespace.
2. Creates a new instance of the InvokableController class.
3. Calls the __invoke method on the InvokableController instance.
4. Renders the

Snippet By Dev Dojo

·

Created June 12th, 2021

·

Report Snippet