Search⌘ K
AI Features

Routing: Mapping URLs

Explore how to implement routing in PHP by mapping URLs to script files, enabling clean URLs without .php extensions. Understand how to use the $_SERVER variable and PATH_INFO to dynamically load pages and improve the structure and flexibility of PHP projects.

Purpose of routing

Another important concept that needs to be discussed before we move on to the next chapter is routing. We will be discussing it because of the following two concerns:

  1. We have .php in every page URL, which does not look great. A URL that looks like /pictures instead of /pictures.php needs to be used.
  2. The URL of a page is tied to its file name, which does not give us flexibility. A URL that looks like /random-number, which shows the output of the random.php script, needs to be used. We also want to be able to rename that file, while the URL stays the same and vice versa ( we should be able
...