Skip to main content
Laravel Basics Tutorial
CHAPTER 04 Beginner

Laravel Routing Basics

Updated: May 14, 2026
20 min read

# CHAPTER 4

Laravel Routing Basics

1. Introduction

In core PHP, if a user wanted to visit an "About Us" page, they had to type the exact physical file path into their browser (e.g., www.site.com/about.php). In modern frameworks, URLs are completely decoupled from physical files. This is managed by the Router. In this chapter, we will learn how to intercept incoming URLs, create clean, SEO-friendly web addresses, and pass dynamic parameters.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define basic GET and POST routes.
  • Return a View directly from a route.
  • Capture dynamic URL parameters (e.g., /user/5).
  • Use Named Routes to prevent broken links.

3. Beginner-Friendly Explanation

Imagine a switchboard operator at a massive corporate headquarters. When you call the company, you don't dial the CEO's direct desk phone. You dial the main public number. The operator answers and asks, "Who do you want to speak with?" If you say "Sales," she unplugs your wire and plugs it into the Sales department. If you say "Support," she plugs you into Support. In Laravel, routes/web.php is the switchboard operator. Every single time a user types a URL (like /about or /contact), Laravel checks the web.php file to see where that URL should be "plugged in."

4. Basic Routing (GET and POST)

All your web routes live in the routes/web.php file. Laravel's Route class accepts two arguments: the URL pattern, and a "Closure" (an anonymous function) that executes when someone visits that URL.

A GET Route (Visiting a page):

php
123456
use Illuminate\Support\Facades\Route;

// When a user visits www.site.com/hello
Route::get('/hello', function () {
    return 'Hello, Welcome to Laravel!'; // Returns plain text
});

Returning a View: Instead of plain text, we usually return a beautiful HTML View.

php
12345
// When a user visits www.site.com/about
Route::get('/about', function () {
    // This looks for resources/views/about.blade.php
    return view('about'); 
});

5. Dynamic Route Parameters

What if you have 10,000 user profiles? You cannot manually write a route for /user/1, /user/2, etc. You must capture the ID dynamically using {} curly braces.
php
123456789
// The {id} acts as a wild-card placeholder
Route::get('/user/{id}', function ($id) {
    return 'Now viewing user profile number: ' . $id;
});

// Multiple Parameters
Route::get('/post/{post_id}/comment/{comment_id}', function ($post_id, $comment_id) {
    return "Viewing comment $comment_id on post $post_id";
});

6. Fallback Routes (Custom 404 Pages)

If a user types a URL that doesn't exist (/super-secret-page), Laravel throws a 404 error. You can intercept this and show a custom error page.
php
123
Route::fallback(function () {
    return view('errors.custom_404'); // Render your custom HTML
});

7. Named Routes (The Best Practice)

Imagine you have an HTML link to your profile page: <a href="/user/profile">Profile</a>. Later, your boss says, "Change the URL to /account/profile." You now have to search through 50 HTML files and change every single link manually! Named Routes solve this. You assign a nickname to a route.

In web.php:

php
1234
// We name the route 'profile.show'
Route::get(&#039;/account/my-profile-page', function () {
    return view(&#039;profile');
})->name(&#039;profile.show');

In your HTML (Blade) View:

html
12
<!-- Generate the URL dynamically using the route() helper function -->
<a href="{{ route(&#039;profile.show') }}">Go to Profile</a>

*Why this is magic:* If your boss changes the URL to /my-cool-account, you only change it in web.php. Because the HTML link relies on the nickname (profile.show), every single link on your website instantly updates automatically!

8. Backend Workflow: Routing to a Controller

In the real world, you do not write massive functions inside web.php. It gets too messy. The Router's only job is to point the traffic to a Controller.
php
1234
use App\Http\Controllers\UserController;

// When a user visits /users, hand the traffic to the 'index' method of UserController
Route::get(&#039;/users', [UserController::class, 'index']);

*(We will cover this deeply in the next chapter).*

9. Best Practices

  • Always Use Named Routes: Never hardcode URLs like <a href="/about"> in your views. Always use <a href="{{ route('about') }}">. It will save you from catastrophic link breakage when you redesign your site.

10. Common Mistakes

  • Method Not Allowed Error: If you create an HTML form with method="POST", but your web.php file defines the route as Route::get('/submit'), Laravel will crash and throw a 405 Method Not Allowed exception. POST traffic must be caught by a Route::post().

11. Exercises

  1. 1. Write a dynamic route that captures a string {category} and a number {id}, and returns text saying: "Viewing Category: X, Item: Y".

12. Coding Challenges

  • Challenge: Open routes/web.php in a Laravel project. Create a route for /contact that returns a view. Name the route contact.us.

13. MCQs with Answers

Question 1

In which file do you define the URLs for standard web pages in a Laravel application?

Question 2

What is the primary benefit of using "Named Routes" (e.g., ->name('profile')) in Laravel?

14. Interview Questions

  • Q: Explain how dynamic routing parameters work in Laravel. How do you pass an ID from the URL into the route's closure or controller?
  • Q: Differentiate between Route::get and Route::post. Give an example of a user action that would require each.

15. FAQs

Q: I see a file called api.php in the routes folder. What is that? A: Laravel separates web traffic (humans using browsers) from API traffic (mobile apps asking for JSON). web.php automatically applies Sessions and CSRF security, which browsers need. api.php is lightweight and stateless, designed strictly for JSON responses.

16. Summary

In Chapter 4, we took control of our application's traffic. The routes/web.php file acts as the ultimate switchboard operator, catching every incoming URL and deciding what happens next. By utilizing dynamic parameters, we can serve thousands of unique profiles with a single line of code. By enforcing the use of Named Routes, we guarantee our application's links will never break, no matter how often we redesign our URLs.

17. Next Chapter Recommendation

Our switchboard operator is currently doing all the heavy lifting. We need to hand the work off to the Waiters. Proceed to Chapter 5: Working with Controllers.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·