Complete the code to define a single action controller class in Laravel.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class WelcomeController extends Controller { public function [1](Request $request) { return view('welcome'); } }
In Laravel, a single action controller uses the __invoke method to handle requests.
Complete the route definition to use a single action controller in Laravel.
use App\Http\Controllers\WelcomeController; Route::get('/', [1]::class);
When using a single action controller, you pass the controller class name directly to the route.
Fix the error in the single action controller method signature.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ContactController extends Controller { public function [1](Request $request, $id) { return view('contact', ['id' => $id]); } }
The __invoke method can accept parameters like $id if needed for single action controllers.
Fill both blanks to create a route that uses a single action controller with middleware.
use App\Http\Controllers\DashboardController; Route::get('/dashboard', [1]::class)->[2]('auth');
The route uses the controller class name and the middleware method to apply middleware.
Fill all three blanks to define a single action controller class with a constructor and the invoke method.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ProfileController extends Controller { private $service; public function [1](UserService $service) { $this->service = [2]; } public function [3](Request $request) { return view('profile', ['data' => $this->service->getProfile()]); } }
The constructor is named __construct, it assigns the passed $service to the property, and the single action method is __invoke.