URL Management(网址管理)

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

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

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

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

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

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

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

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

    1. array(
    2. ......
    3. 'components'=>array(
    4. ......
    5. 'urlManager'=>array(
    6. 'urlFormat'=>'path',
    7. ),
    8. ),
    9. );

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

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

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

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

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

    1. '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 .
    • 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.
    • : 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.

    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:

    1. <ParamName:ParamPattern>

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

    1. array(
    2. 'posts'=>'post/list',
    3. 'post/<id:\d+>'=>'post/read',
    4. 'post/<year:\d{4}>/<title>'=>'post/read',
    5. )
    • 调用$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。请注意,没有规则适用。

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

    如果GET参数传递到是以上所要求的一项规则,其他参数将出现在查询字符串。例如,如果我们调用$this->createUrl('post/read',array('id'=>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获得) 。

    {$blockquote1}

    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:

    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 create, update 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 , we can create a URL
    /index.php/comments?page=2.

    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.:

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

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

    Note that 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.

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

    1. Options +FollowSymLinks
    2. IndexIgnore */*
    3. RewriteEngine on
    4.  
    5. # if a directory or a file exists, use it directly
    6. RewriteCond %{REQUEST_FILENAME} !-f
    7.  
    8. # otherwise forward it to index.php
    9. RewriteRule . index.php

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

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

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

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

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

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

    1. array(
    2. // 一个标准的URL规则,将 '/' 对应到 'site/index'
    3. '' => 'site/index',
    4.  
    5. // 一个标准的URL规则,将 '/login' 对应到 'site/login', 等等
    6. '<action:(login|logout|about)>' => 'site/<action>',
    7.  
    8. // 一个自定义URL规则,用来处理 '/Manufacturer/Model'
    9. array(
    10. 'class' => 'application.components.CarUrlRule',
    11. 'connectionID' => 'db',
    12. ),
    13.  
    14. // 一个标准的URL规则,用来处理 'post/update' 等
    15. '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    16. ),

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

    {$pre12}

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

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

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

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