DEV Community

Cover image for Laravel 9 - Single Action Controllers
Sandro Jhuliano Cagara
Sandro Jhuliano Cagara

Posted on

Laravel 9 - Single Action Controllers

If you want to create a controller with just one action, you can use __invoke() method and even create "invokable" controller.

Route:

Route::get('path/{id}', 'MyClass');
Enter fullscreen mode Exit fullscreen mode

Artisan:

php artisan make:controller MyClass --invokable
Enter fullscreen mode Exit fullscreen mode

Controller:

class MyClass extends Controller
{
    public function __invoke($id)
    {
        return view('parent.child', [
            'data' => MyModel::findOrFail($id)
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)