The reason I ask this is because isn't a PHP script a route? For example, if you have an article.php then your route is simply http://mysite.com/article.php.
Why further abstract away the concept of a route when it already exists as a simple file?
To understand what a router does, you must first understand what a rewrite engine is. From the Wikipedia article (emphasis mine):
A rewrite engine is software that modifies a web URL's appearance (URL rewriting). Rewritten URLs (sometimes known as short, fancy URLs, or search engine friendly - SEF) are used to provide shorter and more relevant-looking links to web pages. The technique adds a degree of separation between the files used to generate a web page and the URL that is presented to the World.
When a rewrite engine is used you don't have a 1:1 correlation between the URL and a PHP script. An example from the same article:
can be rewritten as:
If you want rewritten URLs, you need some kind of routing, as routing is the process of taking the URL, breaking it into components and deciding what is the actual script to call. The documentation page for the standard router of the Zend Framework explains the process as:
Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.
Most PHP frameworks nowadays are based on the MVC pattern, and on an MVC framework the process goes something like this*:
index.phpgets the URL and starts the routing process.
Matching parameters to controllers and methods usually employs matching via regular expressions to be able to handle complex and dynamic routing patterns, known as routes. Good examples of routes can be found on CodeIgniter's URI Routing documentation page:
$route['journals'] = "blogs";
$route['blog/joe'] = "blogs/users/34";
$route['product/(:any)'] = "catalog/product_lookup";
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
$route array holds the patterns as keys and the resulting actions as values in
*This is not intended as a description of the canonical process, just an oversimplified explanation.