好好爱自己!

With PHP frameworks, why is the “route” concept used?

 

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?

shareimprove this question
 

To understand what a router does, you must first understand what a rewrite engine is. From theWikipedia 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 an 1:1 correlation between the URL and a PHP script. An example from the same article:

http://example.com/wiki/index.php?title=Page_title

can be rewritten as:

http://example.com/wiki/Page_title

There are various benefits to using the technique. Since PHP is usually tightly coupled with Apache, the most commonly used rewrite engine is Apache's mod_rewrite.

If you want rewritten URLs, you need some kind of routing, as routing is the process of taking the URL, braking 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*:

  1. Browser is pointed to a URL,
  2. Web server catches request and forwards it to a common entry point, usually an index.phpscript,
  3. index.php gets the URL and starts the routing process.
  4. URL is decomposed into parameters, where the first is the controller, second is the action method, and the rest are considered dynamic parameters,
  5. If a controller class matching the first parameter exists, an controller object is instantiated,
  6. The action method which is usually a function of the controller object is called and its return is what actually returned to the browser.

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";

The $route array holds the patterns as keys and the resulting actions as values in controller/action_method/dynamic_parameter format.

*This is not intended as a description of the canonical process, just an oversimplified explanation.

shareimprove this answer
 
8  
There should be a badge for describing a mvc router without using any derivative of "dispatch"... – YannisNov 29 '11 at 8:40 
    
Thanks @marcv ... – Yannis Apr 11 '14 at 17:33
posted @   立志做一个好的程序员  阅读(237)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示