Yii Framework Forum: Url管理组件 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Url管理组件 Rate Topic: -----

#1 User is offline   persistence 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 02-January 10

Posted 21 July 2011 - 04:46 AM

url管理组件主要提供2个功能:
1、根据用户输入的url,解析出处理这个请求的路由——由哪个Controller的哪个Action来处理,同时将url中的部分参数添加到$_GET参数中。在每个web框架中都需要一个这样的组件来进行路由分发的工作。
2、根据路由和参数数组创建出url。在视图层可以对url进行硬编码,即直接写死url地址,但是这往往缺乏灵活性,为后期的维护带来成本。
array(
'components'=>array(
'urlFormat'=>'path',
'rules'=>array(
'/art/<cate:\w+>/<key:\d+>/<id:\d+>/<p:\d+>'=>'article/<cate>/<key>',
'post/<id:\d+>/<title:.*?>'=>'post/view',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
);
如上是一个url管理组件的配置,一共有3条规则。下图以第一条规则为例,说明了url解析和url创建的2个功能。对于每个路由规则,CUrlManager都会创建一个CUrlRule对象来处理这条规则对应的这个2个功能,所以说有一条规则就会有几个CUrlRule对象。所以CUrlRule才是url管理的核心所在,接下来分析CUrlRule的工作原理。

Posted Image

每条url路由规则由一个CUrlRule对象来进行处理,接下来以如下路由规则为例:'/art/<cate:\w+>/<key:\d+>/<id:\d+>/<p:\d+>'=>'article/<cate>/<key>',说明url解析和url创建的处理过程。每个CUrlRule对象处理url的过程可以分为3个阶段:
1、 初始化CUrlRule对象
在CUrlRule对象的构造函数中,会初始化6个重要的成员变量:
Posted Image
2、 解析url
解析url的工作分3步走:a、根据pattern规则,解析出url中的各个字段;b、根据references对路由中的引用字段进行替换;c、将params中指定的字段添加到$_GET数组中
3、 创建url
创建url的工作分3步走:a、根据routePattern规则,解析出输入的路由中各个字段;b、将输入的参数数组和上一步解析的数组进行合并;c、用合并后的数组对template进行替换
Posted Image
0

#2 User is offline   netyum 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 14-January 10

Posted 21 July 2011 - 10:38 PM

好帖,要顶
0

#3 User is offline   巡洋艦 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 316
  • Joined: 18-December 09
  • Location:Beijing, China

Posted 25 July 2011 - 04:47 AM

额,学习一下!
0

#4 User is offline   bugmelody 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 78
  • Joined: 09-December 09

Posted 26 July 2011 - 09:33 AM

这个强大,希望能做个urlmanager源码解析,毕竟这是yii的核心,理解了urlmanager对整个yii的帮助很大
我现在看这部分看的头疼
0

#5 User is offline   winds 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 25-March 11
  • Location:http://www.dlf5.com

Posted 03 August 2011 - 10:36 PM

强大!马克
0

#6 User is offline   Rock Cat 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-February 11

Posted 07 December 2011 - 02:42 AM

我也来学习下!
楼主辛苦了
0

#7 User is offline   sshaoyee 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 08-February 12

Posted 08 February 2012 - 07:48 AM

辛苦了 。
0

#8 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 599
  • Joined: 27-December 10
  • Location:china

Posted 10 February 2012 - 02:51 AM

:lol: 要是学习能到这个地步 啥都不愁了 很刻苦 顶一个
0

#9 User is offline   duanliyang 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 30-October 12

Posted 30 October 2012 - 11:52 AM

:lol: 写的很好呀
0

#10 User is offline   小小吃货 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 31-March 13

Posted 31 March 2013 - 08:00 PM

马克一下,以后来学习
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users