Laravel - Controllers - Basic Controllers
Let's take a look at an example of a basic controller. Note that the controller extends the base controller class included with Laravel: App\Http\Controllers\Controller
:
User::findOrFail($id)
]);
}
}
You can define a route to this controller method like so:
use App\Http\Controllers\UserController;
Route::get('/user/{id}', [UserController::class, 'show']);
When an incoming request matches the specified route URI, the show
method on the App\Http\Controllers\UserController
class will be invoked and the route parameters will be passed to the method.
Controllers are not required to extend a base class. However, you will not have access to convenient features such as themiddleware
andauthorize
methods.