yii2框架随笔29
生活随笔
收集整理的這篇文章主要介紹了
yii2框架随笔29
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
今天我們來(lái)看UrlRule.php
<?php /*** @link http://www.yiiframework.com/* @copyright Copyright (c) 2008 Yii Software LLC* @license http://www.yiiframework.com/license/*/ namespace yii\web; use Yii; use yii\base\Object; use yii\base\InvalidConfigException; /*** UrlRule represents a rule used by [[UrlManager]] for parsing and generating URLs.* urlrule代表由[[urlmanager]]用于解析和生成的URL規(guī)則* To define your own URL parsing and creation logic you can extend from this class* and add it to [[UrlManager::rules]] like this:* 定義自己的網(wǎng)址解析和創(chuàng)建邏輯,您可以從這個(gè)類擴(kuò)展添加到[規(guī)則] ] [ urlmanager::像這樣:* ~~~* 'rules' => [* ['class' => 'MyUrlRule', 'pattern' => '...', 'route' => 'site/index', ...],* // ...* ]* ~~~** rule中class的默認(rèn)值是yii\web\UrlRule** @author Qiang Xue <qiang.xue@gmail.com>* @since 2.0*/ class UrlRule extends Object implements UrlRuleInterface {/*** Set [[mode]] with this value to mark that this rule is for URL parsing only* 集[ [model] ]與這個(gè)值,以標(biāo)記這條規(guī)則是唯一的網(wǎng)址解析*/const PARSING_ONLY = 1;/*** Set [[mode]] with this value to mark that this rule is for URL creation only* 集[ [model] ]與這個(gè)值,以標(biāo)記這條規(guī)則是唯一的網(wǎng)址解析*/const CREATION_ONLY = 2;/*** @var string the name of this rule. If not set, it will use [[pattern]] as the name.* 這個(gè)規(guī)則的名稱。如果沒(méi)有設(shè)置,它將使用[ [模式] ]作為名稱。*/public $name;/*** @var string the pattern used to parse and create the path info part of a URL.* 用來(lái)解析和創(chuàng)建一個(gè)鏈接的路徑信息的模式。* @see host*/public $pattern;/*** @var string the pattern used to parse and create the host info part of a URL (e.g. `http://example.com`).* @see pattern* 用于解析和創(chuàng)建一個(gè)URL的主機(jī)信息的模式(例如`http://example.com`)。*/public $host;/*** @var string the route to the controller action* 控制器作用的路徑*/public $route;/*** @var array the default GET parameters (name => value) that this rule provides.* 該規(guī)則提供的默認(rèn)參數(shù)(名稱=值)。* When this rule is used to parse the incoming request, the values declared in this property* will be injected into $_GET.* 當(dāng)這個(gè)規(guī)則被用來(lái)解析傳入的請(qǐng)求時(shí),在這個(gè)屬性中聲明的值將注入$_GET。*/public $defaults = [];/*** @var string the URL suffix used for this rule.* 用于此規(guī)則的網(wǎng)址后綴。* For example, ".html" can be used so that the URL looks like pointing to a static HTML page.* 例如,“HTML”可以使URL看起來(lái)像指向一個(gè)靜態(tài)HTML頁(yè)面。* If not, the value of [[UrlManager::suffix]] will be used.* 例如,“HTML”可以使URL看起來(lái)像指向一個(gè)靜態(tài)HTML頁(yè)面。*/public $verb;/*** @var integer a value indicating if this rule should be used for both request parsing and URL creation,parsing only, or creation only.* 一個(gè)值,該值表示,如果該規(guī)則應(yīng)用于請(qǐng)求分析和鏈接創(chuàng)建,只分析或創(chuàng)建。** If not set or 0, it means the rule is both request parsing and URL creation.* 如果沒(méi)有設(shè)置或0,這意味著規(guī)則是請(qǐng)求解析和鏈接創(chuàng)建。* If it is [[PARSING_ONLY]], the rule is for request parsing only.* 如果是[[parsing_only]],規(guī)則是只要求解析。* If it is [[CREATION_ONLY]], the rule is for URL creation only.* 如果是[[creation_only]],規(guī)則是URL只有創(chuàng)造。*/public $mode;/*** @var boolean a value indicating if parameters should be url encoded.* 返回一個(gè)boolean值,該值指示如果參數(shù)應(yīng)該是網(wǎng)址編碼。*/public $encodeParams = true;/*** @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL.* 生成一個(gè)新的網(wǎng)址的模板。這是源于[[pattern]] ,并用于產(chǎn)生網(wǎng)址。*/private $_template;/*** @var string the regex for matching the route part. This is used in generating URL.* 該路線的部分匹配正則表達(dá)式。這是用來(lái)產(chǎn)生網(wǎng)址。*/private $_routeRule;/*** @var array list of regex for matching parameters. This is used in generating URL.* list of regex for matching parameters. This is used in generating URL.*/private $_paramRules = [];/*** @var array list of parameters used in the route.* 路由的參數(shù)存儲(chǔ)數(shù)組*/private $_routeParams = [];/*** Initializes this rule.*/public function init(){if ($this->pattern === null) {throw new InvalidConfigException('UrlRule::pattern must be set.');}if ($this->route === null) {throw new InvalidConfigException('UrlRule::route must be set.');}if ($this->verb !== null) {// 將verb變成數(shù)組,并將器內(nèi)容全部大寫if (is_array($this->verb)) {foreach ($this->verb as $i => $verb) {$this->verb[$i] = strtoupper($verb);}} else {$this->verb = [strtoupper($this->verb)];}}if ($this->name === null) {$this->name = $this->pattern;}$this->pattern = trim($this->pattern, '/');$this->route = trim($this->route, '/');if ($this->host !== null) {// host存在$this->host = rtrim($this->host, '/');$this->pattern = rtrim($this->host . '/' . $this->pattern, '/');} elseif ($this->pattern === '') {// pattern為空$this->_template = '';$this->pattern = '#^$#u';return;} elseif (($pos = strpos($this->pattern, '://')) !== false) {// 存在'://'字符串if (($pos2 = strpos($this->pattern, '/', $pos + 3)) !== false) {// 找到'://'之后的第一個(gè)'/'的位置,并截取之前的字符串作為host$this->host = substr($this->pattern, 0, $pos2);} else {$this->host = $this->pattern;}} else {$this->pattern = '/' . $this->pattern . '/';}/*** $rule的結(jié)構(gòu)如下* [* 'route'=>'PUT,POST <controller:\w+>/<id>'* 'verb'=>['PUT','POST'],* 'pattern'=>'<controller:\w+>/<id>'* ]*/if (strpos($this->route, '<') !== false && preg_match_all('/<(\w+)>/', $this->route, $matches)) {// 匹配不帶正則表達(dá)式的路由配置,并放入_routeParams中存起來(lái)// 如上的例子中,$matches[1]=['id']foreach ($matches[1] as $name) {$this->_routeParams[$name] = "<$name>";}}$tr = ['.' => '\\.','*' => '\\*','$' => '\\$','[' => '\\[',']' => '\\]','(' => '\\(',')' => '\\)',];$tr2 = [];/*** 匹配帶正則表達(dá)式的路由配置* PREG_PATTERN_ORDER* 結(jié)果排序?yàn)?matches[0]保存完整模式的所有匹配, $matches[1] 保存第一個(gè)子組的所有匹配,以此類推。** PREG_SET_ORDER* 結(jié)果排序?yàn)?matches[0]包含第一次匹配得到的所有匹配(包含子組), $matches[1]是包含第二次匹配到的所有匹配(包含子組)的數(shù)組,以此類推。** PREG_OFFSET_CAPTURE* 如果這個(gè)標(biāo)記被傳遞,每個(gè)發(fā)現(xiàn)的匹配返回時(shí)會(huì)增加它相對(duì)目標(biāo)字符串的偏移量。* 注意這會(huì)改變matches中的每一個(gè)匹配結(jié)果字符串元素,使其成為一個(gè)第0個(gè)元素為匹配結(jié)果字符串,第1個(gè)元素為 匹配結(jié)果字符串在subject中的偏移量。** 如果沒(méi)有給定排序標(biāo)記,假定設(shè)置為PREG_PATTERN_ORDER。** 如果$this->pattern是'<controller:\w+>/<id:\d+>'* 則$matches為[* [['<controller:\w+>', 0], ['controller', 1], ['\w+', 12]],* [['<id:\d+>', 17], ['id', 18], ['\d+', 21]]* ]*/if (preg_match_all('/<(\w+):?([^>]+)?>/', $this->pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {foreach ($matches as $match) {// 以第一條記錄為例// $name = 'controller'$name = $match[1][0];// $pattern = '\w+'// 如果正則表達(dá)式的匹配值為空,則默認(rèn)為'[^\/]+'$pattern = isset($match[2][0]) ? $match[2][0] : '[^\/]+';if (array_key_exists($name, $this->defaults)) {$length = strlen($match[0][0]);$offset = $match[0][1];if ($offset > 1 && $this->pattern[$offset - 1] === '/' && $this->pattern[$offset + $length] === '/') {$tr["/<$name>"] = "(/(?P<$name>$pattern))?";} else {$tr["<$name>"] = "(?P<$name>$pattern)?";}} else {// str['<controller>'] = '(?P<controller>\w+)'$tr["<$name>"] = "(?P<$name>$pattern)";}if (isset($this->_routeParams[$name])) {$tr2["<$name>"] = "(?P<$name>$pattern)";} else {$this->_paramRules[$name] = $pattern === '[^\/]+' ? '' : "#^$pattern$#u";}}}// 如果$this->pattern是'<controller:\w+>/<id:\d+>'// 則$this->_template是'<controller>/<id>'$this->_template = preg_replace('/<(\w+):?([^>]+)?>/', '<$1>', $this->pattern);// $this->pattern最終是'#^(?P<controller>\w+)/(?P<id>\d+)$#u'$this->pattern = '#^' . trim(strtr($this->_template, $tr), '/') . '$#u';if (!empty($this->_routeParams)) {$this->_routeRule = '#^' . strtr($this->route, $tr2) . '$#u';}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/taokai/p/5480005.html
總結(jié)
以上是生活随笔為你收集整理的yii2框架随笔29的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。