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.