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. MyBatis中Like语句使用方式

    oracle数据库: SELECT * FROM user WHERE name like CONCAT('%',#{name},'%') 或 SELECT * FROM user WHERE nam ...

  2. Form表单的post 和get跳转区别

    post是隐示请求 ----- 安全 get显示请求不安全,会在URL上显示路径和参数

  3. CSS样式 初学

    CSS样式 参考网站: CSS用法:3种 一:直接样式表 如<p style="color:red;">这是一个段落</p> 二:内部样式表 如:<s ...

  4. 444A/CF

    题目链接[http://codeforces.com/problemset/problem/444/A] 题意:给出一个无向图,找出一个联通子图,定义密度#=v(顶点值的和)/e(边值的和). 条件: ...

  5. View与ViewGroup有什么区别?

    百度知道:http://zhidao.baidu.com/link?url=B5MFOzDlww8soYqr5CL5FldH4sXD6eumS1XTRn8XEh8gu4mKjQdPkJSLIBt7u_ ...

  6. HDU 3966 Aragorn's Story 动态树 树链剖分

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. 提升html5的性能体验系列之一避免切页白屏

    窗体切换白屏的现实问题 HTML5的性能比原生差很多,比如切页时白屏.列表滚动不流畅.下拉刷新和上拉翻页卡顿.在低端Android手机上,很多原生App常用的功能和体验效果都很难使用HTML5技术模拟 ...

  8. Entity Framework 学习中级篇1—EF支持复杂类型的实现

    本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作. 通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和Las ...

  9. mysql慢查询问题

    [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的时候,查询的记录数才几万条,但查询的速度非常慢,大概要4~5分钟左右 [处理过程] 1)explain 首先怀疑索引没 ...

  10. FragmentActivity与Fragment两者交互方法简介(转)

    FragmentActivity与Fragment两者交互方法简介 分类: Fragment 2014-07-07 18:17 88人阅读 评论(0) 收藏 举报 在Android4.0后很多时候我们 ...