PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

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