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?

asked Nov 29 '11 at 5:23
Ryan

7272820
 

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.

answered Nov 29 '11 at 5:57
Yannis

30.5k36151197
 
8  
There should be a badge for describing a mvc router without using any derivative of "dispatch"... – Yannis♦Nov 29 '11 at 8:40 
    
Thanks @marcv ... – Yannis♦ Apr 11 '14 at 17:33

With PHP frameworks, why is the “route” concept used?的更多相关文章

  1. [Angular2 Router] Configuring a Home Route and Fallback Route - Learn An Essential Routing Concept

    In this tutorial we are going to learn how to configure the Angular 2 router to cover some commonly ...

  2. The Top 10 Javascript MVC Frameworks Reviewed

    Over the last several months I have been in a constant search for the perfect javascript MVC framewo ...

  3. Notes of Linked Data concept and application - TODO

    Motivation [反正债多了不愁,再开个方向.] Data plays a core role in most business systems, data storage and retrie ...

  4. Top JavaScript Frameworks, Libraries & Tools and When to Use Them

    It seems almost every other week there is a new JavaScript library taking the web community by storm ...

  5. 大数据日志分析产品——SaaS Cloud, e.g. Papertrail, Loggly, Sumo Logic;Open Source Frameworks, e.g. ELK stack, Graylog;Enterprise Products, e.g. TIBCO LogLogic, IBM QRadar, Splunk

    Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brough ...

  6. Awesome Go精选的Go框架,库和软件的精选清单.A curated list of awesome Go frameworks, libraries and software

    Awesome Go      financial support to Awesome Go A curated list of awesome Go frameworks, libraries a ...

  7. Application Request Route实现IIS Server Farms集群负载详解

    序言 随着公司业务的发展,后台业务就变的越来越多,然而服务器的故障又像月经一样,时不时的汹涌而至,让我们防不胜防.那么后台的高可用,以及服务器的处理能力就要做一个横向扩展的方案,以使后台业务持续的稳定 ...

  8. .net core 源码解析-mvc route的注册,激活,调用流程(三)

    .net core mvc route的注册,激活,调用流程 mvc的入口是route,当前请求的url匹配到合适的route之后,mvc根据route所指定的controller和action激活c ...

  9. angular路由——ui.route

    angular路由 使用案例 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

随机推荐

  1. Linux 终端下颜色的输出

    在命令行下也能产生五颜六色的字体和图案,只需要加上一些颜色代码,例如 echo -e "\033[41;36m 红底绿字\033[0m" 其中41的位置代表底色, 36的位置是代表 ...

  2. hibernate ——关联关系

    1.一对一单向外键关联 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC ...

  3. navicat连接oracle时发现 ORA-12737 set CHS16GBK

    oracle安装目录下找到目录:\product\11.2.0\dbhome_1\BIN, 将箭头标注的三个文件(截图中为navicat中的目录,已经测试成功,亲们可以参考)从目录中拷贝纸navica ...

  4. ligerUI调用$.ligerDialog.open弹出窗口,关闭后无法获取焦点问题

    1:调用父窗口某一个文件框,获取焦点,   parent.window.document.getElementByIdx_x("roleName").focus(); 2:关闭父窗 ...

  5. 【iOS 】UIView 中有一个autoresizingMask的属性

    在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum  ...

  6. centos yum源问题

    在配置CentOS-6.0-x86_64-bin-DVD2.iso作为本地yum源的时候,碰到相当多的问题: -----------------------------------------  问题 ...

  7. Python实现删除目录下相同文件

    让我们来分析一下这个问题:首先,文件个数非常多,手工查找是不现实的,再说,单凭我们肉眼,在几千张图片或文件里面找到完全相同的难度也是很大的.所以要用程序实现.那么用程序怎么实现呢?根据什么判断两个文件 ...

  8. 初探JavaScript魅力(五)

    JS简易日历    innerHTML <title>无标题文档</title> <script> var neirong=['一','二','三','四','五' ...

  9. HDU 3038 How Many Answers Are Wrong (并查集)---并查集看不出来系列-1

    Problem Description TT and FF are ... friends. Uh... very very good friends -________-bFF is a bad b ...

  10. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...