编程学习网 > PHP技术 > Yii1 > Yii框架官方指南系列43——专题:URL(创建、路由、美化及自定义)
2014
11-07

Yii框架官方指南系列43——专题:URL(创建、路由、美化及自定义)

Web应用程序完整的URL管理包括两个方面。首先, 当用户请求约定的URL,应用程序需要解析 它变成可以理解的参数。第二,应用程序需求提供一种创造URL的方法,以便创建的URL应用程序可以理解的。对于Yii应用程序,这些通过CUrlManager辅助完成。

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。

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)

上面的数组包含了一系列自定义的选项设置,在版本1.1.0中,下面的选项是有效的:

  • urlSuffix: URL所使用的后缀设置规则,默认是null,使用的是CUrlManager::urlSuffix的设置.
  • caseSensitive: 规则是否是大小学敏感的,默认是null,使用的是CUrlManager::caseSensitive的默认值.
  • defaultParams: 规则提供的GET参数的默认值 (name=>value). 当规则用来解析输入请求的时候,该属性中声明的值将会注入$_GET.
  • matchValue: 当创建一个URL的时候GET参数的值是否和规则中相应的子模式相匹配. 默认是null, 意味着使用的是CUrlManager::matchValue中的值. 如果属性值是false, 意味着如果路由和参数名与给定的匹配那么该规则被用来创建一个URL. 如果该属性被设置为true,那么给定的参数值一定也要和相应的子模式参数值相匹配. 注意如果设置该属性的值为true将会降低性能

使用命名参数

一个规则可以关联一些GET参数。这些GET参数作为特殊令牌出现在规则的模式中,如下所示:

<ParamName:ParamPattern>;

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获得) 。

createurl方法所产生的是一个相对地址。为了得到一个绝对的url ,我们可以用前缀yii">注:使用的URL规则将降低应用的性能。这是因为当解析请求的URL ,[ CUrlManager ]尝试使用每个规则来匹配它,直到某个规则可以适用。因此,高流量网站应用应尽量减少其使用的URL规则。

参数化路由

从版本1.0.5开始, 我们可能会用到命名参数作为路由规则的一部分. 这就允许一个规则可以基于匹配规范被用来匹配多个路由,这也许还会帮助减少应用所需的规则的数目,从而提高整体的性能.

我们使用如下示例规则来说明如何通过命名参数来参数化路由:

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',
)

在上面的示例中, 我们在路由规则中使用了两个命名参数: _c_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.

使用上面的规则, URL /index.php/post/123/create 将会被解析为 post/create 使用GET参数 id=123. And given the route comment/list and GET parameter page=2, we can create a URL /index.php/comments?page=2.

参数化主机名

从版本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

注意在正式测试之前确保apache开启了rewrite模块,在ubuntu中开启的方式如下:

cd /etc/apache2/mods-enabled

sudo ln -s ../mods-available/rewrite.load rewrite.load

sudo service apache2 restart

现在,如果我们调用$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页面。 注意,这种用法要求规则类在所有其他规则的最后声明。

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取