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. SQL 合并列值和拆分列值

    合并列值 表结构,数据如下: id value ----- ------ aa bb aaa bbb ccc 需要得到结果: id values ------ ----------- aa,bb aa ...

  2. wpf CollectionViewSource与ListBox的折叠、分组显示,及输入关键字 Filter的筛选

    在wpf中虽然ObservableCollection<T>作为ListBox的Itemsource,很好,很强大!但是CollectionViewSource与ListBox才是天作之合 ...

  3. 转 Oracle 12c 使用scott等普通用户的方法

    一.前言 最近电脑上安装了oracle 12c数据库,想体验下新特性.安装完后,便像11g一样在dos窗口进行下面的操作: SQL*Plus: Release 12.1.0.2.0 Productio ...

  4. NoSql的产生

    主流的关系型数据库:Microsoft SQLServer, IBM DB2, Oracle, MySQL, Microsoft Access, Sybase,IBM Informix 随着互联网we ...

  5. Android OpenGL ES(二)OpenGL ES管道(Pipeline) .

    大部分图形系统都可以比作工厂中的装配线(Assemble line)或者称为管道(Pipeline).前一道的输出作为下道工序的输入.主CPU发出一个绘图指令,然后可能由硬件部件完成坐标变换,裁剪,添 ...

  6. CentOS下载及版本选择-CentOS LiveCD、LiveDVD和BinDVD区别

    1.CentOS系统镜像有两个,安装系统只用到第一个镜像即CentOS-6.x-i386-bin-DVD1.iso(32位)或者CentOS-6.x-x86_64-bin-DVD1.iso(64位), ...

  7. zend frameword 基本语法

    #resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"#resources.frontCo ...

  8. java 子类继承父类成员变量的隐藏、实现方法的重写

    成员变量的隐藏和方法的重写 Goods.java public class Goods { public double weight; public void oldSetWeight(double ...

  9. play1.x vs play2.x 对比(转)

    个人看到对比play1.x和play2.x比较的文章中,写的最深入,最清晰的一个.转自:http://freewind.me/blog/20120728/965.html 为了方便群中的Play初学者 ...

  10. C# 通过接口 post 请求

    /// <summary> /// 提交数据请求 方法一 /// </summary> /// <param name="POSTURL">请求 ...