MVC Lesson 8: Routing, HTTP Methods & Path Normalization

Let’s break down three of the most important concepts: Routing, HTTP Methods, and Path Normalization. We’ll also touch on how classes and views are loaded in an MVC pattern.

This blog is based on a custom PHP MVC project you can explore on GitHub:
👉 GitHub – phpiggy (Section 10)


📌 What is Routing in PHP MVC?

Imagine this:

When a user visits http://yourdomain.com/posts/42, how does your app know that it should display post #42?

That’s where routing comes in.

🛤 What does a router do?

A router is like a traffic manager. It looks at the URL and decides:

  • Which controller class to use
  • Which method inside that controller to call
  • What parameters (like 42) to pass in

🔧 Without routing, your app would be messy:

You’d have something like this everywhere:

if ($_SERVER['REQUEST_URI'] === '/posts/42') {
    // Call this function manually
}

With routing, you can define a cleaner rule like:

/posts/{id} → use the PostController and run its show() method with id = 42

That way, the router handles the logic, not your raw PHP pages.


🔁 Understanding HTTP Methods (GET, POST, etc.)

In web development, the same URL can mean different actions depending on how it’s used.

For example:

MethodURLAction
GET/postsShow a list of posts
GET/posts/42Show one post
POST/postsSubmit a new post
PUT/posts/42Update a post
DELETE/posts/42Delete a post

Your router can check both the URL and the HTTP method to make the correct decision.

✅ A beginner tip:
When working with HTML forms, use GET for searching or viewing data, and POST for sending or saving data.


🧹 What is Path Normalization?

The problem:

When users visit URLs, they can be messy. For example:

  • //posts//42/
  • /posts/../users
  • /posts/./42

These aren’t clean paths and can even create security issues.

🧼 The solution:

Path normalization is the process of cleaning and simplifying a URL path.

Normalization will:

  • Remove duplicate slashes (///)
  • Remove dots (. means “this folder”, .. means “go back a folder”)
  • Make everything consistent, so routes match exactly what you define

So /posts/../users becomes /users, which is predictable and secure.


🏗 How Controllers and Methods Work in MVC

In an MVC framework:

  • Model = Data layer (e.g. fetching from a database)
  • View = What the user sees (HTML templates)
  • Controller = Middle layer that controls logic and connects model to view

✋ What is a controller?

A controller is just a PHP class that handles a specific section of your site. For example:

  • PostController → handles blog posts
  • UserController → handles user accounts

When a route like /posts/42 is requested, the router might call something like:

(new PostController)->show(42);

That’s just PHP’s way of saying:

“Create an object of the PostController class and run the show method with the value 42.”


🖼 Loading Views: Showing HTML to Users

Once the controller has done its job (maybe it fetched a blog post from a database), it needs to show the result to the user.

This is done by loading a view.

👀 What’s a view?

A view is just a PHP/HTML file that displays content. It’s where you use templates like:

<h1><?= $post['title']; ?></h1>

The controller sends data to the view, like this:

require 'views/posts/show.view.php';

The view can then use that data to show it in a nice format.


🧠 Summary — How It All Connects

Let’s walk through a simple example:

  1. A user goes to /posts/42
  2. The router reads the URL and figures out:
    • This is a GET request
    • It matches the route /posts/{id}
    • It should call PostController@show with id = 42
  3. The PostController fetches post #42 from the database
  4. It sends the data to a view file like show.view.php
  5. The user sees the blog post on their screen

Everything behind the scenes is handled by your MVC framework!


🧭 Where to Go From Here?

Here are some good next steps:

  • Explore how to register routes in your index.php or router file
  • See how controller classes are organized inside the app/ directory
  • Look at how the view files are structured and loaded

Your repo already has the working code, so now you also understand why and how each part works!

ComponentRole
index.phpMain entry point of the app
bootstrap.phpPrepares the app: loads classes, sets up routes
App.phpCore of the app: handles route registration and dispatch
RouterMatches URL and calls correct controller

💡 The Full Analogy Explained in Simple Terms:

1. index.php – The Butler

Think of index.php like a butler standing at the entrance of the hotel or building.

  • The job of the butler (index.php) is simple:
    • When someone rings the bell (makes a request to the website), the butler says, “Alright, let me call the receptionist (bootstrap.php) to take care of this.”
  • The butler’s task is to trigger the start of the app by including the bootstrap.php file, which sets everything up.

2. bootstrap.php – The Receptionist

Now, the receptionist (bootstrap.php) gets involved.

  • The receptionist’s role is to set up the app:
    • They know which rooms (controllers and methods) are available.
    • They also know which guests (routes) want to go to which rooms.
  • The receptionist hands over the guest’s request (which route they want to access) to the person who handles the map (App.php).

Think of it like this:

  • The receptionist registers the guest’s (user’s) request by saying:
    • “If anyone asks for / (Home), I will direct them to the HomeController and tell them to go to the home method.”

3. App.php – The Room Service Agent (Map Handler)

The Room Service Agent (App.php) now has to take action.

  • The room service agent’s role is to map out the guest’s request (URL) and pass it to the manager (Router).
  • So, when the guest (user) asks to go to a certain room (route), the room service agent (App.php) says:
    • “Here’s the guest’s request — please check with the manager (Router) to see if the room exists, and whether the guest can be allowed in.”

4. Router.php – The Manager

Now, we get to the manager (Router.php), who is responsible for checking if the requested room (controller and method) exists.

Manager’s Job:

  • The manager receives the request, checks if the room (controller) exists, and if it does:
    • They create a gate pass (instantiate the class) for the guest (user) to access that room.
    • Then, they escort the guest to the right room (method of controller).
  • If the manager (Router) can’t find the room (controller and method), they either:
    • Show the guest (user) the 404 page (not found), or
    • Send them to a default room (e.g., an error page).

5. Final Flow Summary (All Together)

  1. User Request: A user goes to the website (e.g., /).
  2. Butler (index.php): The butler receives the request and calls the receptionist (bootstrap.php).
  3. Receptionist (bootstrap.php): The receptionist knows how to handle requests and hands the request to the room service agent (App.php).
  4. Room Service Agent (App.php): The agent passes the request to the manager (Router.php) to check if the room (controller and method) exists.
  5. Manager (Router.php): The manager checks if the room exists. If yes, they create a gate pass (instance) and send the guest (user) to the correct room (method of the controller).

🧠 Simplified Step-by-Step Process for Freshers:

  1. index.php is the butler. It’s where everything starts.
    • It knows the receptionist (bootstrap.php) will handle the request.
  2. bootstrap.php is the receptionist. It sets up the app and tells the butler how to direct the request.
    • It says, “If someone asks for /, send them to the HomeController and use the home method.”
  3. App.php is the room service agent. The agent has a map of where all the rooms are.
    • It knows what rooms (controllers and methods) exist and gives the manager (Router.php) the request to check if the room is available.
  4. Router.php is the manager. The manager checks if the room (controller) and method (action) are available.
    • If they are, the manager creates a gate pass (instance) and takes the guest (user) to the right room (method).
    • If the room is not available, the manager tells the guest (user) the room does not exist (404 error).

🌟 Conclusion:

Your analogy works really well! To make it even clearer:

  • index.php = Butler: Receives the request and passes it on.
  • bootstrap.php = Receptionist: Registers the routes and knows who should handle them.
  • App.php = Room Service Agent: Passes the request to the manager (Router).
  • Router.php = Manager: Checks if the room exists and sends the guest to the right room (controller and method).

Each part plays a specific role in making sure the user gets the right response to their request!