Laravel Routing: Routes, Parameters and Controllers

Laravel ~ 8 minutes

# Welcome to Laravel routing

Hello and welcome! In this tutorial, we will learn routing in Laravel. Routing is how your app decides what to do when a visitor opens a web address.

Think of a route as a road sign. When someone visits /products, the sign tells Laravel which code to run. Every Laravel app uses routes, so this is a very important skill. You can read the official guide at laravel.com/docs/routing.

What you will learn

  • How to make simple routes
  • How to use route parameters
  • How to name routes and group them
  • How to connect routes to controllers

Before you start

  • PHP 8.2 or newer
  • Composer installed on your computer
Create a new Laravel project
Code Copied to Clipboard

# Wait for the install

This command downloads Laravel and all the packages it needs. It may take a few minutes, so please be patient. When it finishes, you will have a new folder named shop-app. This folder holds your whole project.

Now, please move inside this folder. All our next commands must run from the project root. This is the folder that contains the file named artisan.

Enter the project
Code Copied to Clipboard

# Start the development server

Laravel has a helper tool called Artisan. It gives you many useful commands. One of them starts a small web server for development. Please run it now. Then open http://127.0.0.1:8000 in your browser. You should see the Laravel welcome page. Keep this terminal open while you work. Please open a second terminal window for the next commands.

Serve the application
Code Copied to Clipboard

# Where routes live

Your web routes are in the file routes/web.php. Please open it in your editor. You will see one route already. It shows the welcome page.

A basic route has three parts. First is the HTTP method, such as get or post. Second is the URI, which is the address path. Third is the action, which can be a simple function. When a visitor opens that address, Laravel runs the function and sends back whatever it returns. The next block adds two simple routes. Please add them at the bottom of the file.

routes/web.php - basic routes
Code Copied to Clipboard

No need to restart the server

When you change a route file, you do not need to restart php artisan serve. Just save the file and refresh your browser. Visit http://127.0.0.1:8000/hello to see your new message. If you see a 404 Not Found page, please check the spelling of the address. Also check that you saved the file.

# Route parameters

Often the address contains a changing value. For example, /products/5 shows product number five. We call this a route parameter. We write it inside curly braces, like {id}. Laravel passes the value into your function, so you can use it.

The parameter name in the braces should match the variable in your function in order. In the next block, {id} becomes $id. Try visiting /products/5 in your browser. You should see the number in the message.

A route with a parameter
Code Copied to Clipboard

# Optional parameters and rules

Sometimes a parameter is not required. Add a question mark after its name, like {category?}. Then give your function a default value, so it works when the part is missing.

You can also limit what a parameter accepts. The where method uses a pattern. For example, whereNumber('id') allows only digits. If someone visits /orders/abc, Laravel shows a 404 page. This keeps wrong data out of your code. The next block shows both ideas.

Optional and constrained parameters
Code Copied to Clipboard
Order of routes matters

Laravel checks routes from top to bottom, and it uses the first match. So place specific routes before general ones. For example, /products/new must come before /products/{id}. If you reverse them, Laravel thinks the word "new" is an id.

Quick tips

  • Put fixed addresses first
  • Put parameter routes after them
  • Use constraints like whereNumber to avoid mixed matches

Please remember this rule. It solves many confusing bugs.

# Named routes

Writing the same address in many places is risky. If you change it later, you must fix every place. A better way is to give the route a name. Then you build links with the name, and Laravel creates the correct address for you.

Use the name method to set a name. Use the route() helper to make the link. The next block shows a named route and a redirect that uses it. If you later change the address, the name keeps working.

Naming a route
Code Copied to Clipboard

# Route groups

When many routes share the same start, you can put them in a group. A group can share a URL prefix, a name prefix and middleware. This saves typing and keeps the file neat.

In the next block, every route inside the group starts with /admin, and every name starts with admin.. The auth middleware means only logged in users can enter. Middleware is like a guard at the door. It checks the visitor before the route runs.

Grouping routes
Code Copied to Clipboard

# Why we use controllers

Functions inside the route file are fine for small tests. But in a real app, the file becomes long and messy. The better way is to use controllers. A controller is a class that holds the logic for a group of related routes. The route file only points to it.

Laravel can create a controller for you with Artisan. This saves time and avoids typing mistakes. Please run the command in the next block in your second terminal window. It makes a file named ProductController.php.

Make a controller
Code Copied to Clipboard

# Edit the controller

Please open the new file at app/Http/Controllers/ProductController.php. Inside the class, we add two methods. Each method is one action. The method index shows a list. The method show shows one item, and it receives the $id from the route.

In a real app, these methods would read data from the database and return a view. To keep our lesson simple, we return plain text.

ProductController.php
Code Copied to Clipboard

# Point routes to the controller

Now we connect the routes to our methods. We give the route an array. The first item is the controller class, and the second is the method name. Remember to import the controller at the top of routes/web.php with a use line.

After you save, visit /products and /products/3. You should see the two messages from the controller. Now your route file is short, and your logic lives in the right place.

routes/web.php - controller routes
Code Copied to Clipboard

# See all your routes

As your app grows, you may forget which routes exist. Artisan can print a neat table of them all. It shows the method, the address, the name and the action. This is very useful for checking your work and for finding mistakes. Please run it now and look for your new product routes in the list.

List all routes
Code Copied to Clipboard

Resource controllers save time

Most apps need the same seven actions for a thing: list, show, create, store, edit, update and delete. Laravel can make all these routes with one line, Route::resource('products', ProductController::class);. You can also create the controller for it with php artisan make:controller ProductController --resource. Please learn the basic routes first, and then try this shortcut. You can read more in the resource controller docs.

# Summary and next steps

Very good work! You now understand the basics of Laravel routing. Let us review.

What we learned

  • Routes live in routes/web.php
  • A route connects an address to some code
  • Parameters use curly braces, like {id}
  • Names, groups and constraints keep routes clean
  • Controllers hold the real logic
  • php artisan route:list shows every route

Next steps

  • Learn about middleware in the middleware docs
  • Build views with Blade templates
  • Try resource controllers and connect a database

Please keep practising. Soon routing will feel easy and natural!