Getting to Know Routing and Controllers in Laravel
How a URL finds the code that answers it, and when to reach for a controller.
C Codesakura 1 min 0 comments
Contents
Every page starts with a route: a rule that connects a URL to code. In this part we write our first route and move its logic into a controller.
Your first route
Open routes/web.php and add:
Route::get('/hello', fn () => 'Hello from Laravel');Using a controller
Once the logic grows, move it into a controller so the routes file stays short.
php artisan make:controller ProductControllerRoute::get('/products', [ProductController::class, 'index']);URL parameters
The part of a URL in curly braces is passed to the controller. If it is type-hinted as a model, Laravel looks it up in the database for you.
Part of a series
Laravel from Scratch
0 comments
Log in to join the conversation. Log in