当前位置: 首页 > news >正文

URL Management(网址管理)

为什么80%的码农都做不了架构师?>>>   hot3.png

1. Creating URLs(创建网址) 

虽然URL可被硬编码在控制器的视图(view)文件,但往往可以很灵活地动态创建它们:

$url=$this->createUrl($route,$params);

$this指的是控制器实例; $route指定请求的route 的要求;$params 列出了附加在网址中的GET参数。

默认情况下,URL以get格式使用createUrl 创建。例如,提供$route='post/read'$params=array('id'=>100) ,我们将获得以下网址:

/index.php?r=post/read&id=100

参数以一系列Name=Value通过符号串联起来出现在请求字符串,r参数指的是请求的route 。这种URL格式用户友好性不是很好,因为它需要一些非字字符。

我们可以使上述网址看起来更简洁,更不言自明,通过采用所谓的'path格式,省去查询字符串和把GET参数加到路径信息,作为网址的一部分:

/index.php/post/read/id/100

要更改URL格式,我们应该配置urlManager应用元件,以便createUrl可以自动切换到新格式和应用程序可以正确理解新的网址:

array(
    ......    'components'=>array(
        ......        'urlManager'=>array(
            'urlFormat'=>'path',        ),    ),);

请注意,我们不需要指定的urlManager元件的类,因为它在CWebApplication预声明为CUrlManager。

<blockquote class="提示:此网址通过<a href=" doc="" api="" 1.1="" ccontroller#createurl"="" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: rgb(255, 255, 255); quotes: none; color: rgb(34, 34, 34); font-family: Arial, sans-serif; line-height: 21px;">createurl方法所产生的是一个相对地址。为了得到一个绝对的url ,我们可以用前缀 yii">

提示:此网址通过createurl方法所产生的是一个相对地址。为了得到一个绝对的url ,我们可以用前缀yii: :app()->hostInfo ,或调用createAbsoluteUrl 。

2. User-friendly URLs(用户友好的URL) 

当用path格式URL,我们可以指定某些URL规则使我们的网址更用户友好性。例如,我们可以产生一个短短的URL/post/100 ,而不是冗长/index.php/post/read/id/100。网址创建和解析都是通过CUrlManager指定网址规则。

要指定的URL规则,我们必须设定urlManager 应用元件的属性rules:

array(
    ......    'components'=>array(
        ......        'urlManager'=>array(
            'urlFormat'=>'path',            'rules'=>array(
                'pattern1'=>'route1',                'pattern2'=>'route2',                'pattern3'=>'route3',            ),        ),    ),);

这些规则以一系列的路线格式对数组指定,每对对应于一个单一的规则。路线(route)的格式必须是有效的正则表达式,没有分隔符和修饰语。它是用于匹配网址的路径信息部分。还有route应指向一个有效的路线控制器。

规则可以绑定少量的GET参数。这些出现在规则格式的GET参数,以一种特殊令牌格式表现如下:

'pattern1'=>array('route1', 'urlSuffix'=>'.xml', 'caseSensitive'=>false)

In the above, the array contains a list of customized options. As of version 1.1.0, the following options are available:

  • urlSuffix: the URL suffix used specifically for this rule. Defaults to null, meaning using the value of CUrlManager::urlSuffix.

  • caseSensitive: whether this rule is case sensitive. Defaults to null, meaning using the value of CUrlManager::caseSensitive.

  • defaultParams: the default GET parameters (name=>value) that this rule provides. When this rule is used to parse the incoming request, the values declared in this property will be injected into $_GET.

  • matchValue: whether the GET parameter values should match the corresponding sub-patterns in the rule when creating a URL. Defaults to null, meaning using the value of CUrlManager::matchValue. If this property is false, it means a rule will be used for creating a URL if its route and parameter names match the given ones. If this property is set true, then the given parameter values must also match the corresponding parameter sub-patterns. Note that setting this property to true will degrade performance.

Using Named Parameters

A rule can be associated with a few GET parameters. These GET parameters appear in the rule's pattern as special tokens in the following format:

&lt;ParamName:ParamPattern&gt;

ParamName表示GET参数名字,可选项ParamPattern表示将用于匹配GET参数值的正则表达式。当生成一个网址(URL)时,这些参数令牌将被相应的参数值替换;当解析一个网址时,相应的GET参数将通过解析结果来生成。

我们使用一些例子来解释网址工作规则。我们假设我们的规则包括如下三个:

array(
    'posts'=>'post/list',    'post/<id:\d+>'=>'post/read',    'post/<year:\d{4}>/<title>'=>'post/read',)
  • 调用$this->createUrl('post/list')生成/index.php/posts。第一个规则适用。

  • 调用$this->createUrl('post/read',array('id'=>100))生成/index.php/post/100。第二个规则适用。

  • 调用$this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))生成/index.php/post/2008/a%20sample%20post。第三个规则适用。

  • 调用$this->createUrl('post/read')产生/index.php/post/read。请注意,没有规则适用。

总之,当使用createUrl生成网址,路线和传递给该方法的GET参数被用来决定哪些网址规则适用。如果关联规则中的每个参数可以在GET参数找到的,将被传递给createUrl ,如果路线的规则也匹配路线参数,规则将用来生成网址。

如果GET参数传递到createUrl是以上所要求的一项规则,其他参数将出现在查询字符串。例如,如果我们调用$this->createUrl('post/read',array('id'=>100,'year'=>2008)) ,我们将获得/index.php/post/100?year=2008。为了使这些额外参数出现在路径信息的一部分,我们应该给规则附加/*。 因此,该规则post/<id:\d+>/* ,我们可以获取网址/index.php/post/100/year/2008 。

正如我们提到的,URL规则的其他用途是解析请求网址。当然,这是URL生成的一个逆过程。例如, 当用户请求/index.php/post/100 ,上面例子的第二个规则将适用来解析路线post/read和GET参数array('id'=>100) (可通过$_GET获得) 。

<blockquote class="提示:此网址通过<a href=" doc="" api="" 1.1="" ccontroller#createurl"="" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; quotes: none; background-position: initial initial; background-repeat: initial initial;">createurl方法所产生的是一个相对地址。为了得到一个绝对的url ,我们可以用前缀 yii">

注:使用的URL规则将降低应用的性能。这是因为当解析请求的URL ,[ CUrlManager ]尝试使用每个规则来匹配它,直到某个规则可以适用。因此,高流量网站应用应尽量减少其使用的URL规则。

Parameterizing Routes

Starting from version 1.0.5, we may reference named parameters in the route part of a rule. This allows a rule to be applied to multiple routes based on matching criteria. It may also help reduce the number of rules needed for an application, and thus improve the overall performance.

We use the following example rules to illustrate how to parameterize routes with named parameters:

array(
    '<_c:(post|comment)>/<id:\d+>/<_a:(create|update|delete)>' => '<_c>/<_a>',    '<_c:(post|comment)>/<id:\d+>' => '<_c>/read',    '<_c:(post|comment)>s' => '<_c>/list',)

In the above, we use two named parameters in the route part of the rules: _c and _a. The former matches a controller ID to be either post or comment, while the latter matches an action ID to be createupdate or delete. You may name the parameters differently as long as they do not conflict with GET parameters that may appear in URLs.

Using the aboving rules, the URL /index.php/post/123/create would be parsed as the route post/create with GET parameter id=123. And given the route comment/list and GET parameter page=2, we can create a URL /index.php/comments?page=2.

Parameterizing Hostnames

Starting from version 1.0.11, it is also possible to include hostname into the rules for parsing and creating URLs. One may extract part of the hostname to be a GET parameter. For example, the URL http://admin.example.com/en/profile may be parsed into GET parameters user=admin and lang=en. On the other hand, rules with hostname may also be used to create URLs with paratermized hostnames.

In order to use parameterized hostnames, simply declare URL rules with host info, e.g.:

array(
    'http://<user:\w+>.example.com/<lang:\w+>/profile' => 'user/profile',)

The above example says that the first segment in the hostname should be treated as userparameter while the first segment in the path info should be lang parameter. The rule corresponds to the user/profile route.

Note that CUrlManager::showScriptName will not take effect when a URL is being created using a rule with parameterized hostname.

Also note that the rule with parameterized hostname should NOT contain the sub-folder if the application is under a sub-folder of the Web root. For example, if the application is under http://www.example.com/sandbox/blog, then we should still use the same URL rule as described above without the sub-folder sandbox/blog.

隐藏 index.php

还有一点,我们可以做进一步清理我们的网址,即在URL中藏匿index.php入口脚本。这就要求我们配置Web服务器,以及urlManager应用程序元件。

我们首先需要配置Web服务器,这样一个URL没有入口脚本仍然可以处理入口脚本。如果是Apache HTTP server,可以通过打开网址重写引擎和指定一些重写规则。这两个操作可以在包含入口脚本的目录下的.htaccess文件里实现。下面是一个示例:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

然后,我们设定urlManager元件的showScriptName属性为 false

现在,如果我们调用$this->createUrl('post/read',array('id'=>100)) ,我们将获取网址/post/100。更重要的是,这个URL可以被我们的Web应用程序正确解析。

Faking URL Suffix(伪造URL后缀)

我们还可以添加一些网址的后缀。例如,我们可以用/post/100.html来替代/post/100 。这使得它看起来更像一个静态网页URL。为了做到这一点,只需配置urlManager元件的urlSuffix属性为你所喜欢的后缀。

3. 使用自定义URL规则设置类 

注意: Yii从1.1.8版本起支持自定义URL规则类

默认情况下,每个URL规则都通过CUrlManager来声明为一个CUrlRule对象,这个对象会解析当前请求并根据具体的规则来生成URL。 虽然CUrlRule可以处理大部分URL格式,但在某些特殊情况下仍旧有改进余地。

比如,在一个汽车销售网站上,可能会需要支持类似/Manufacturer/Model这样的URL格式, 其中Manufacturer 和 Model 都各自对应数据库中的一个表。此时CUrlRule就无能为力了。

我们可以通过继承CUrlRule的方式来创造一个新的URL规则类。并且使用这个类解析一个或者多个规则。 以上面提到的汽车销售网站为例,我们可以声明下面的URL规则。

array(
    // 一个标准的URL规则,将 '/' 对应到 'site/index'
    '' => 'site/index', 
    // 一个标准的URL规则,将 '/login' 对应到 'site/login', 等等
    '<action:(login|logout|about)>' => 'site/<action>', 
    // 一个自定义URL规则,用来处理 '/Manufacturer/Model'
    array(
        'class' => 'application.components.CarUrlRule',        'connectionID' => 'db',    ), 
    // 一个标准的URL规则,用来处理 'post/update' 等
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',),

从以上可以看到,我们自定义了一个URL规则类CarUrlRule来处理类似/Manufacturer/Model这样的URL规则。 这个类可以这么写:

class CarUrlRule extends CBaseUrlRule{
    public $connectionID = 'db'; 
    public function createUrl($manager,$route,$params,$ampersand)
    {
        if ($route==='car/index')
        {
            if (isset($params['manufacturer'], $params['model']))
                return $params['manufacturer'] . '/' . $params['model'];            else if (isset($params['manufacturer']))
                return $params['manufacturer'];        }
        return false;  // this rule does not apply
    }
 
    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
    {
        if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))
        {
            // check $matches[1] and $matches[3] to see
            // if they match a manufacturer and a model in the database
            // If so, set $_GET['manufacturer'] and/or $_GET['model']
            // and return 'car/index'
        }
        return false;  // this rule does not apply
    }}

自定义URL规则类必须实现在CBaseUrlRule中定义的两个接口。

  • [CBaseUrlRule::createUrl()|createUrl()]
  • [CBaseUrlRule::parseUrl()|parseUrl()]

除了这种典型用法,自定义URL规则类还可以有其他的用途。比如,我们可以写一个规则类来记录有关URL解析和UEL创建的请求。 这对于正在开发中的网站来说很有用。我们还可以写一个规则类来在其他URL规则都匹配失败的时候显示一个自定义404页面。 注意,这种用法要求规则类在所有其他规则的最后声明。

转载于:https://my.oschina.net/miyae/blog/184834

相关文章:

  • #ifdef 的技巧用法
  • 今日工作总结和心情
  • 平台框架开发的好莱坞大明星原则
  • Python学习笔记 第二课 循环
  • 怎么设置共享文件夹
  • CentOS6.*挂载读写NTFS分区
  • 也谈谈网页上的微创新(多图:百度、淘宝、腾讯等)
  • ipv6 6to4 tunnel测试
  • windows下多线程类CThread
  • 监控UITextView和UITextField的键盘确定事件以及字数控制
  • ora01033 oracle正在初始化或关闭
  • 大韩航空如何成为一流航空——等级文化与称呼的改变
  • ScrollView中嵌套ListView的问题
  • excel常用公式
  • 【crunch bang】 增加“菜单项”
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • Fundebug计费标准解释:事件数是如何定义的?
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • leetcode386. Lexicographical Numbers
  • Netty 4.1 源代码学习:线程模型
  • oschina
  • Python学习之路13-记分
  • spring + angular 实现导出excel
  • 百度小程序遇到的问题
  • 深入浅出webpack学习(1)--核心概念
  • Hibernate主键生成策略及选择
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​520就是要宠粉,你的心头书我买单
  • ###项目技术发展史
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (七)Java对象在Hibernate持久化层的状态
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • .form文件_一篇文章学会文件上传
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .Net8 Blazor 尝鲜
  • .net利用SQLBulkCopy进行数据库之间的大批量数据传递
  • .NET设计模式(11):组合模式(Composite Pattern)
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • /etc/shadow字段详解
  • [Arduino学习] ESP8266读取DHT11数字温湿度传感器数据
  • [BZOJ1877][SDOI2009]晨跑[最大流+费用流]
  • [BZOJ3223]文艺平衡树
  • [C#]科学计数法(scientific notation)显示为正常数字
  • [C++核心编程](四):类和对象——封装
  • [CVPR2021]Birds of a Feather: Capturing Avian Shape Models from Images
  • [exgcd] Jzoj P1158 荒岛野人
  • [Firefly-Linux] RK3568 pca9555芯片驱动详解
  • [hdu 4552] 怪盗基德的挑战书
  • [hive] posexplode函数