DEV Community

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

Posted on

6 3

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more