© Christopher Pitt 2021
C. PittPro PHP 8 MVChttps://doi.org/10.1007/978-1-4842-6957-2_3

3. Building a Router

Christopher Pitt1  
(1)
Verbena Close 1, Stellenberg, Durbanville, Cape Town, South Africa
 

In the last chapter, we put together the code for basic routing. It’s time to package this code up in a way that we can reuse and extend.

What Routers Are Used For

Before we can build a good router, we need to try and understand the problem. When PHP first started out, it was common for applications to rely heavily on the file-based routing that web servers offered.

File-based routing is where you have different files for each URL your website responds to. Say you had a webroot/pages/edit-page.php file; file-based routing would expose that as http://your-website.com/pages/edit-page.php ...

Get Pro PHP 8 MVC: Model View Controller Architecture-Driven Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.