Home>Article>Backend Development> Yii Framework Official Guide Series 43 - Special Topic: URL (Creation, Routing, Beautification and Customization)

Yii Framework Official Guide Series 43 - Special Topic: URL (Creation, Routing, Beautification and Customization)

黄舟
黄舟 Original
2017-02-16 09:29:10 1290browse



#Complete URL management of web applications includes two aspects. First, when the user requests the agreed URL, the application needs to parse it into understandable parameters. Second, the application needs to provide a way to create URLs so that the created URLs can be understood by the application. For Yii applications, these are assisted by CUrlManager.

1. Creating URLs

Although URLs can be hard-coded in the controller's view file, they can often be created dynamically with great flexibility:


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

$this refers to the controller instance; $route specifies the requested route requirements; $params lists the requirements attached to the URLGETparameters in .

By default, URLs are created using createUrl in get format. For example, providing $route='post/read' and $params=array('id'=>100) , we will get the following URL:

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

The parameters are concatenated with a series of Name=Value symbols. Appearing in the request string, the r parameter refers to the requested route. This URL format is not very user-friendly because it requires some non-word characters.

We can make the above URL look cleaner and more self-explanatory by using the so-called 'path format, omitting the query string and adding the GET parameters to the path information as part of the URL:

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

To change the URL format, we should configure the urlManager application element so that createUrl can automatically switch to the new format and the application can correctly understand the new URL:


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

Please note that we do not need to specify the class of the urlManager element because it is pre-declared as CUrlManager in CWebApplication.

The createurl method generates a relative address. In order to get an absolute URL, we can use the prefix yii">. Tip: This URL is a relative address generated by the createurl method. In order to get an absolute URL, we can use the prefix yii:: app()->hostInfo, or call createAbsoluteUrl

2. User-friendly URLs (user-friendly URLs)

When using path format URL, we can specify certain URL rules make our URLs more user-friendly. For example, we can generate a short URL /post/100 instead of the lengthy /index.php/post/read/id/100 URL creation and parsing through CUrlManager. Specify URL rules.

To specify URL rules, we must set the attributes rules of the urlManager application element:


#

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

These rules are specified as an array of route format pairs, each pair corresponding to a single rule. The route (route) format must be a valid regular expression without delimiters and modifiers. For the path information part of the matching URL, the route should point to a valid route controller. The

rule can bind a small number of GET parameters that appear in the rule format, with a special token. The format is as follows:


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

The above array contains a series of custom option settings, in version 1.1.0 , the following options are valid:

  • urlSuffix: The suffix setting rule used by the URL. The default is null, and the setting of CUrlManager::urlSuffix is used.

  • caseSensitive: Whether the rule is sensitive to large and small schools, the default is null, and the default value of CUrlManager::caseSensitive is used.

  • defaultParams: GET provided by the rule The default value of the parameter (name=>value). When the rule is used to parse the incoming request, the value declared in this attribute will be injected into $_GET.

  • matchValue: When created Whether the value of the GET parameter in a URL matches the corresponding sub-pattern in the rule. The default is null, which means that the value in CUrlManager::matchValue is used. If the attribute value is false, it means that if the route and parameter names match The given match then the rule is used to create a URL. If this property is set to true, then the given parameter value must also match the corresponding subpattern parameter value. Note that if the value of this property is set to true Will reduce performance

Use named parameters

A rule can associate some GET parameters. These GET parameters appear as special tokens in the pattern of the rule, as follows:

;

ParamNameindicates the GET parameter name, and the optionalParamPatternindicates what will be used Regular expression matching GET parameter values. When generating a web address (URL), these parameter tokens will be replaced by the corresponding parameter values; when parsing a URL, the corresponding GET parameters will be generated by parsing the results.

We use some examples to explain the URL working rules. Let's assume that our rules include the following three:


##
array( 'posts'=>'post/list', 'post/'=>'post/read', 'post//'=>'post/read', )</pre>
      <p></p>
      <p></p>
      <ul class=" list-paddingleft-2">
       Call
       <li>$this ->createUrl('post/list')<p>Generate<code>/index.php/posts</code>. The first rule applies.<code></code></p></li>Call
       <li>$this->createUrl('post/read',array('id'=>100))<p>Generate<code>/index. php/post/100</code>. The second rule applies.<code></code></p></li>
       <li><p>调用<code>$this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))</code>生成<code>/index.php/post/2008/a%20sample%20post</code>。第三个规则适用。</p></li>
       <li><p>调用<code>$this->createUrl('post/read')</code>产生<code>/index.php/post/read</code>。请注意,没有规则适用。</p></li>
      </ul>
      <p>总之,当使用createUrl生成网址,路线和传递给该方法的GET参数被用来决定哪些网址规则适用。如果关联规则中的每个参数可以在GET参数找到的,将被传递给createUrl ,如果路线的规则也匹配路线参数,规则将用来生成网址。</p>
      <p>如果GET参数传递到createUrl是以上所要求的一项规则,其他参数将出现在查询字符串。例如,如果我们调用<code>$this->createUrl('post/read',array('id'=>100,'year'=>2008))</code>,我们将获得<code>/index.php/post/100?year=2008</code>。为了使这些额外参数出现在路径信息的一部分,我们应该给规则附加<code>/*</code>。 因此,该规则<code>post/<id:\d+>/*</code>,我们可以获取网址<code>/index.php/post/100/year/2008</code>。</p>
      <p>正如我们提到的,URL规则的其他用途是解析请求网址。当然,这是URL生成的一个逆过程。例如, 当用户请求<code>/index.php/post/100</code>,上面例子的第二个规则将适用来解析路线<code>post/read</code>和GET参数<code>array('id'=>100)</code>(可通过<code>$_GET</code>获得) 。</p>
      <blockquote>
       <p>createurl方法所产生的是一个相对地址。为了得到一个绝对的url ,我们可以用前缀<code><code>yii"></code>注:使用的URL规则将降低应用的性能。这是因为当解析请求的URL ,[ CUrlManager ]尝试使用每个规则来匹配它,直到某个规则可以适用。因此,高流量网站应用应尽量减少其使用的URL规则。</p>
      </blockquote>
      <h4>参数化路由</h4>
      <p>从版本1.0.5开始, 我们可能会用到命名参数作为路由规则的一部分. 这就允许一个规则可以基于匹配规范被用来匹配多个路由,这也许还会帮助减少应用所需的规则的数目,从而提高整体的性能.</p>
      <p>我们使用如下示例规则来说明如何通过命名参数来参数化路由:</p>
      <p><br></p>
      <p></p>
      <pre class="brush:php;toolbar:false">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', )</pre>
      <p></p>
      <p>在上面的示例中, 我们在路由规则中使用了两个命名参数:<code>_c</code>和<code>_a</code>. The former matches a controller ID to be either<code>post</code>or<code>comment</code>, while the latter matches an action ID to be<code>create</code>,<code>update</code>or<code>delete</code>. You may name the parameters differently as long as they do not conflict with GET parameters that may appear in URLs.</p>
      <p>使用上面的规则, URL<code>/index.php/post/123/create</code>将会被解析为<code>post/create</code>使用GET参数<code>id=123</code>. And given the route<code>comment/list</code>and GET parameter<code>page=2</code>, we can create a URL<code>/index.php/comments?page=2</code>.</p>
      <h4>参数化主机名</h4>
      <p>从版本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<code>//m.sbmmt.com/</code>may be parsed into GET parameters<code>user=admin</code>and<code>lang=en</code>. On the other hand, rules with hostname may also be used to create URLs with paratermized hostnames.</p>
      <p>In order to use parameterized hostnames, simply declare URL rules with host info, e.g.:</p>
      <p><br></p>
      <p></p>
      <pre class="brush:php;toolbar:false">array( 'http://<user:\w+>.example.com/<lang:\w+>/profile' => 'user/profile', )</pre>
      <p></p>
      <p>The above example says that the first segment in the hostname should be treated as<code>user</code>parameter while the first segment in the path info should be<code>lang</code>parameter. The rule corresponds to the<code>user/profile</code>route.</p>
      <p>Note that CUrlManager::showScriptName will not take effect when a URL is being created using a rule with parameterized hostname.</p>
      <p>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<code>//m.sbmmt.com/</code>, then we should still use the same URL rule as described above without the sub-folder<code>sandbox/blog</code>.</p>
      <h4>隐藏<code>index.php</code></h4>
      <p>还有一点,我们可以做进一步清理我们的网址,即在URL中藏匿<code>index.php</code>入口脚本。这就要求我们配置Web服务器,以及urlManager应用程序元件。</p>
      <p>我们首先需要配置Web服务器,这样一个URL没有入口脚本仍然可以处理入口脚本。如果是Apache HTTP server,可以通过打开网址重写引擎和指定一些重写规则。这两个操作可以在包含入口脚本的目录下的<code>.htaccess</code>文件里实现。下面是一个示例:</p>
      <pre class="brush:php;toolbar:false">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</pre>
      <p>然后,我们设定urlManager元件的showScriptName属性为<code>false</code>。</p>
      <p>注意在正式测试之前确保apache开启了rewrite模块,在ubuntu中开启的方式如下:</p>
      <p>cd /etc/apache2/mods-enabled</p>
      <p>sudo ln -s ../mods-available/rewrite.load rewrite.load</p>
      <p>sudo service apache2 restart</p>
      <p>现在,如果我们调用<code>$this->createUrl('post/read',array('id'=>100))</code>,我们将获取网址<code>/post/100</code>。更重要的是,这个URL可以被我们的Web应用程序正确解析。</p>
      <h4>Faking URL Suffix(伪造URL后缀)</h4>
      <p>我们还可以添加一些网址的后缀。例如,我们可以用<code>/post/100.html</code>来替代<code>/post/100</code>。这使得它看起来更像一个静态网页URL。为了做到这一点,只需配置urlManager元件的urlSuffix属性为你所喜欢的后缀。</p>
      <h3>3. 使用自定义URL规则设置类</h3>
      <blockquote>
       <p><strong>注意:</strong>Yii从1.1.8版本起支持自定义URL规则类</p>
      </blockquote>
      <p>默认情况下,每个URL规则都通过CUrlManager来声明为一个CUrlRule对象,这个对象会解析当前请求并根据具体的规则来生成URL。 虽然CUrlRule可以处理大部分URL格式,但在某些特殊情况下仍旧有改进余地。</p>
      <p>比如,在一个汽车销售网站上,可能会需要支持类似<code>/Manufacturer/Model</code>这样的URL格式, 其中<code>Manufacturer</code>和<code>Model</code>都各自对应数据库中的一个表。此时CUrlRule就无能为力了。</p>
      <p>我们可以通过继承CUrlRule的方式来创造一个新的URL规则类。并且使用这个类解析一个或者多个规则。 以上面提到的汽车销售网站为例,我们可以声明下面的URL规则。</p>
      <p><br></p>
      <p></p>
      <pre class="brush:php;toolbar:false">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>', ),</pre>
      <p></p>
      <p>从以上可以看到,我们自定义了一个URL规则类<code>CarUrlRule</code>来处理类似<code>/Manufacturer/Model</code>这样的URL规则。 这个类可以这么写:</p>
      <p><br></p>
      <p></p>
      <pre class="brush:php;toolbar:false">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 } }</pre>
      <p></p>
      <p>自定义URL规则类必须实现在CBaseUrlRule中定义的两个接口。</p>
      <ul class=" list-paddingleft-2">
       <li><p>[CBaseUrlRule::createUrl()|createUrl()]</p></li>
       <li><p>[CBaseUrlRule::parseUrl()|parseUrl()]</p></li>
      </ul>
      <p>除了这种典型用法,自定义URL规则类还可以有其他的用途。比如,我们可以写一个规则类来记录有关URL解析和UEL创建的请求。 这对于正在开发中的网站来说很有用。我们还可以写一个规则类来在其他URL规则都匹配失败的时候显示一个自定义404页面。 注意,这种用法要求规则类在所有其他规则的最后声明。</p>
      <p>以上就是Yii框架官方指南系列43——专题:URL(创建、路由、美化及自定义)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!<br></p>
      <p><br></p>
     </div>
     <div class="nphpQianMsg">
      <div class="clear"></div>
     </div>
     <div class="nphpQianSheng">
      <span>Statement:</span>
      <div>
       The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
      </div>
     </div>
    </div>
    <div class="nphpSytBox">
     <span>Previous article:<a class="dBlack" title="Yii Framework Official Guide Series 42 - Special Topic: Authentication and Authorization" href="//m.sbmmt.com/m/faq/352143.html">Yii Framework Official Guide Series 42 - Special Topic: Authentication and Authorization</a></span>
     <span>Next article:<a class="dBlack" title="Yii Framework Official Guide Series 42 - Special Topic: Authentication and Authorization" href="//m.sbmmt.com/m/faq/352145.html">Yii Framework Official Guide Series 42 - Special Topic: Authentication and Authorization</a></span>
    </div>
    <div class="nphpSytBox2">
     <div class="nphpZbktTitle">
      <h2>Related articles</h2>
      <em><a href="//m.sbmmt.com/m/article.html" class="bBlack"><i>See more</i><b></b></a></em>
      <div class="clear"></div>
     </div>
     <ul class="nphpXgwzList">
      <li><b></b><a href="//m.sbmmt.com/m/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a>
       <div class="clear"></div></li>
     </ul>
    </div>
   </div>
   <div class="nphpFoot">
    <div class="nphpFootBg">
     <ul class="nphpFootMenu">
      <li><a href="//m.sbmmt.com/m/"><b class="icon1"></b><p>Home</p></a></li>
      <li><a href="//m.sbmmt.com/m/course.html"><b class="icon2"></b><p>Course</p></a></li>
      <li><a href="//m.sbmmt.com/m/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li>
      <li><a href="//m.sbmmt.com/m/login"><b class="icon5"></b><p>My</p></a></li>
      <div class="clear"></div>
     </ul>
    </div>
   </div>
   <div class="nphpYouBox" style="display: none;">
    <div class="nphpYouBg">
     <div class="nphpYouTitle">
      <span onclick="$('.nphpYouBox').hide()"></span>
      <a href="//m.sbmmt.com/m/"></a>
      <div class="clear"></div>
     </div>
     <ul class="nphpYouList">
      <li><a href="//m.sbmmt.com/m/"><b class="icon1"></b><span>Home</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/course.html"><b class="icon2"></b><span>Course</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/article.html"><b class="icon3"></b><span>Article</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/wenda.html"><b class="icon4"></b><span>Q&A</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/dic.html"><b class="icon6"></b><span>Dictionary</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/course/type/99.html"><b class="icon7"></b><span>Manual</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/xiazai/"><b class="icon8"></b><span>Download</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/faq/zt" title=""><b class="icon12"></b><span>Topic</span>
        <div class="clear"></div></a></li>
      <div class="clear"></div>
     </ul>
    </div>
   </div>
   <div class="nphpDing" style="display: none;">
    <div class="nphpDinglogo">
     <a href="//m.sbmmt.com/m/"></a>
    </div>
    <div class="nphpNavIn1">
     <div class="swiper-container nphpNavSwiper1">
      <div class="swiper-wrapper">
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/">Home</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/article.html" class="hover">Article</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/wenda.html">Q&A</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/course.html">Course</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/faq/zt">Topic</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/xiazai">Download</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/game">Game</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/dic.html">Dictionary</a>
       </div>
       <div class="clear"></div>
      </div>
     </div>
     <div class="langadivs">
      <a href="javascript:;" class="bg4 bglanguage"></a>
      <div class="langadiv">
       <a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a>
       <a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a>
       <a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a>
       <a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a>
       <a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a>
       <a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a>
       <a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a>
       <a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a>
      </div>
     </div>
    </div>
   </div>
   <!--顶部导航 end-->
  </div>
  <link rel="stylesheet" id="_main-css" href="//m.sbmmt.com/m/static/css/viewer.min.css" type="text/css" media="all">
 </body>
</html>