What you need

  • Computer and internet: a laptop or desktop and a stable connection
  • System: Windows 10 or 11, macOS or Linux
  • Tools: PHP 8.3 or newer, Composer, Git and a code editor (the Laravel installation guide lists the official one-command installer)
  • Before this: Your first Laravel route

Download only from each tool's official site and check the address before installing.

The situation

A teammate added an "About" page to a Laravel app and says it works on their laptop. On yours, http://127.0.0.1:8000/about-us shows 404 Not Found. Your job: find out why using evidence, fix it, and explain the cause in one sentence.

This exercise practises the skills from Your first Laravel route. Try it on your own first; the hints and the solution are at the end.

Ewoi

Resist the urge to change things at random. A 404 is a precise message: no route matched. Your job is to find out why not.

Set up

Work in a fresh practice project, not in laravel-app: your laravel-app already has an About route from the lesson, and two routes with the same name would hide the bug you are hunting. From your workspace:

cd ~/ewoi-learning
composer create-project laravel/laravel route-exercise
cd route-exercise
Command What it does
cd ~/ewoi-learning Moves you into your learning workspace
composer create-project laravel/laravel route-exercise Creates a new Laravel 13 app in a folder called route-exercise (a minute or two)
cd route-exercise Moves you into it. Every command below runs from here

Replace everything in routes/web.php with exactly this code (it contains the bug on purpose):

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('home');
});

Route::post('/about', function () {
    return view('about');
})->name('about');

Create resources/views/home.blade.php:

<h1>Welcome</h1>
<a href="/about-us">About us</a>

And create resources/views/about.blade.php:

<h1>About us</h1>
<p>We build things.</p>

Start the server with php artisan serve, open http://127.0.0.1:8000, and click the link. You should see a 404 page. That is the bug, working as planned.

Before you change anything, save the broken version so you can compare later:

git init
git add .
git commit -m "Start the broken route exercise"

Your task

  1. Find every reason the page does not load. (There is more than one.)
  2. Fix them so the link opens the About page.
  3. Make the link survive a future change of address.
  4. Write one sentence for each cause, as you would in a pull request.

How to check your work

  • php artisan route:list --path=about shows a line starting with GET|HEAD for about and the name about.
  • Clicking the link on the home page shows "About us".
  • Changing the route's address (for example to /who-we-are) does not break the link.

Want an automatic check? Our public repo has a small test for this exercise: https://github.com/ewoibuilds/practical-software-development/tree/main/step-02-fix-the-broken-route. Copy starter/tests/Feature/AboutPageTest.php into your project's tests/Feature/ folder and run php artisan test. On the broken version it reports failures; when you have fixed everything, every test passes.

Hints (open one at a time)

Hint 1Run php artisan route:list --path=about. On the broken version it shows POST about. Compare the method and the address with what your browser requests. The Network tab in your browser's developer tools shows the method too.
Hint 2A browser opening a link always sends a GET request. What method does the route expect?
Hint 3Read the link's address and the route's address character by character.
Hint 4The lesson's section on named routes shows how links can follow a route when its address changes.

Ask your AI assistant

I am doing a Laravel exercise: a link returns 404. Here is my route and my link: [paste both]. Do not tell me the answer. Ask me two questions that help me find the cause myself.

Solution

Try for at least 20 minutes before reading on.

There are two causes:

Cause Evidence
The route accepts only POST, but clicking a link sends GET route:list shows POST about
The link points to /about-us, the route listens on /about The browser requests /about-us; no route has that path

The fix:

Route::get('/about', function () {
    return view('about');
})->name('about');

And the link in resources/views/home.blade.php, built from the route's name so it always follows the route:

<a href="{{ route('about') }}">About us</a>

Save the fix as its own commit, so the history shows the broken version and the repair:

git add .
git commit -m "Fix the About route method and link"

The pull request sentences: "The About route only accepted POST, but links send GET, so it never matched." and "The link was hard-coded to /about-us while the route was /about; it now uses route('about') so it follows the route."

Ewoi

Notice how the evidence (route:list and the Network tab) gave you both causes without guessing. That habit is worth more than this exercise.

Go further

  • Change the route's address to /who-we-are and confirm the link still works.
  • Add a second page with a route parameter, /team/{name}, that greets the person by name. What happens with /team/ and no name?

Check your understanding

1. A browser opens a link. Which HTTP method does it send?

Links and typed addresses send GET. Forms can send POST.

2. Which command shows every route with its method, address and name?

route:list is the first thing to check when a page returns 404.

3. Why use route('about') instead of href="/about"?

Named routes keep links correct when addresses change.

Get the code on GitHub

Found this useful?

Every lesson stays free. Tap the thumbs up so we know what works, or say thanks with a soda or a coffee: it pays for hosting and new lessons.

Buy me a sodaBuy me a coffee

Want more on this?

Tell us which part was not clear, ask for a follow-up lesson, or request a lesson on any topic. We read every request.

I would like
Only to tell you when it is ready.

WhatsApp

Join the channelChat with us

Free lessons and updates in the channel. Questions about learning or training? Chat with us.