Request Flow
Browser Request
↓
IIS / web.config
↓
Check: is it a real file or folder?
↓
Yes --------------------→ Serve directly with IIS
No
↓
Rewrite to public/index.php
↓
Read URL + HTTP method
↓
Search route table
↓
Match controller + action
↓
Extract route parameters
↓
Call controller method
↓
Return view / response
What the Router Does
The router acts as the traffic director for the application. Its job is to inspect the incoming request, compare it against the route definitions, and decide which controller and action method should handle the request.
- Receives the request path
- Checks the HTTP method such as GET or POST
- Compares the request against defined routes
- Finds the matching controller and action
- Extracts parameters from the URL
- Calls the correct controller method
Example Route
['GET', '#^/posts/(\\d+)$#', 'posts', 'show', [1]]
What this means
GET — This route responds only to GET requests.
#^/posts/(\\d+)$# — This pattern matches a URL such as /posts/5.
'posts' — This maps to the PostsController.
'show' — This maps to the show() action method.
[1] — This tells the router to capture the first matched value and pass it into the action.
Example Match
If the browser requests:
/posts/5
The router may resolve the request like this:
Controller: PostsController Action: show Parameter: 5
Which leads to something like:
$controller = new PostsController();
$controller->show(5);
Why the Rewrite Rule Matters
The IIS rewrite rule sends requests to index.php only when the request is
not an existing file or directory.
/docs/routing-snapshot.pdfloads directly in the browser/posts/5is handled by the MVC framework
This creates a clean separation between static public files and dynamic application routes.
Step-by-Step Flow
1. The browser sends a request
A user visits a URL such as /posts/5. The browser sends that request to the server.
2. IIS receives the request
IIS checks whether the request points to a real file or folder. If it does, IIS serves it directly. If it does not, the request is rewritten to the front controller.
3. The front controller starts the application
The request enters public/index.php, which acts as the main entry point for the
application.
4. The router checks the route table
The framework compares the request path and HTTP method against the routes defined in the application.
5. A route match is found
If the request matches a route pattern, the framework identifies the controller, action, and any parameters.
6. The controller method is called
The controller action handles the request and prepares the response, often by loading data and rendering a view.
Why This Is Useful
This routing approach gives the framework a clean and expandable structure.
- Clean URL design
- Centralized request handling
- Easy controller organization
- Support for route parameters
- Better separation of concerns
Key Idea
A route is a rule that says: when this kind of request comes in, send it to this controller method.
Snapshot Summary
- IIS serves real files directly
- Other requests are rewritten to
index.php - The router checks the HTTP method and URL
- Matching routes select a controller and action
- Route parameters are passed into the method
- The controller returns the final response