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:

1Route::get('user/{id}', 'ShowProfile');

Artisan:

1php artisan make:controller ShowProfile --invokable

Controller:

1class ShowProfile extends Controller
2{
3 public function __invoke($id)
4 {
5 return view('user.profile', [
6 'user' => User::findOrFail($id)
7 ]);
8 }
9}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord