Laravel

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 ProductController
Route::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
  1. 1Setting Up Your First Laravel Project
  2. 2Getting to Know Routing and Controllers in Laravel
  3. 3Building a REST API with Laravel from Scratch
Share X Facebook WhatsApp Telegram

0 comments

Log in to join the conversation. Log in

Related tutorials

Laravel

Building a REST API with Laravel from Scratch

1 min Series · Part 3 of 3

Laravel

Setting Up Your First Laravel Project

1 min Series · Part 1 of 3

Laravel

Secure File Uploads in Laravel

1 min