Single Action Controllers
If you would like to define a controller that only handles a single action, you may place a single __invoke
method on the controller:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\User;
class ShowProfile extends Controller
{
/**
* Show the profile for the given user.
*
* @param int $id
* @return View
*/
public function __invoke($id)
{
return view('user.profile', ['user' => User::findOrFail($id)]);
}
}
When registering routes for single action controllers, you do not need to specify a method:
Route::get('user/{id}', 'ShowProfile');
You may generate an invokable controller by using the --invokable
option of the make:controller
Artisan command:
php artisan make:controller ShowProfile --invokable