Single Action Controller: Invokable Controller

Posted: 2025/03/11

In Laravel, I can create a Controller with only one method __invoke() by using

php artisan make:controller SearchController -i

Or I can manually add the public function __invoke to the Controller.

class SearchController extends Controller
{
    public function __invoke()
    {
        return ('This is for Search function');
    }
}

Then in the route file web.php, I can call the Controller directly, don't need to declare the method.

Route::get('/search', [SearchController::class]);

Noted here in case I forget and can quickly refer back later.

No comments yet

Leave your comment

Search
Side Widget
You can put anything you want inside of these side widgets. They are easy to use, and feature the Bootstrap 5 card component!