日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

yii2框架随笔29

發布時間:2023/11/27 生活经验 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 yii2框架随笔29 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天我們來看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規則* To define your own URL parsing and creation logic you can extend from this class* and add it to [[UrlManager::rules]] like this:* 定義自己的網址解析和創建邏輯,您可以從這個類擴展添加到[規則] ] [ urlmanager::像這樣:* ~~~* 'rules' => [*     ['class' => 'MyUrlRule', 'pattern' => '...', 'route' => 'site/index', ...],*     // ...* ]* ~~~** rule中class的默認值是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] ]與這個值,以標記這條規則是唯一的網址解析*/const PARSING_ONLY = 1;/*** Set [[mode]] with this value to mark that this rule is for URL creation only* 集[ [model] ]與這個值,以標記這條規則是唯一的網址解析*/const CREATION_ONLY = 2;/*** @var string the name of this rule. If not set, it will use [[pattern]] as the name.* 這個規則的名稱。如果沒有設置,它將使用[ [模式] ]作為名稱。*/public $name;/*** @var string the pattern used to parse and create the path info part of a URL.* 用來解析和創建一個鏈接的路徑信息的模式。* @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* 用于解析和創建一個URL的主機信息的模式(例如`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.* 該規則提供的默認參數(名稱=值)。* When this rule is used to parse the incoming request, the values declared in this property* will be injected into $_GET.* 當這個規則被用來解析傳入的請求時,在這個屬性中聲明的值將注入$_GET。*/public $defaults = [];/*** @var string the URL suffix used for this rule.* 用于此規則的網址后綴。* For example, ".html" can be used so that the URL looks like pointing to a static HTML page.* 例如,“HTML”可以使URL看起來像指向一個靜態HTML頁面。* If not, the value of [[UrlManager::suffix]] will be used.* 例如,“HTML”可以使URL看起來像指向一個靜態HTML頁面。*/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.* 一個值,該值表示,如果該規則應用于請求分析和鏈接創建,只分析或創建。** If not set or 0, it means the rule is both request parsing and URL creation.* 如果沒有設置或0,這意味著規則是請求解析和鏈接創建。* If it is [[PARSING_ONLY]], the rule is for request parsing only.* 如果是[[parsing_only]],規則是只要求解析。* If it is [[CREATION_ONLY]], the rule is for URL creation only.* 如果是[[creation_only]],規則是URL只有創造。*/public $mode;/*** @var boolean a value indicating if parameters should be url encoded.* 返回一個boolean值,該值指示如果參數應該是網址編碼。*/public $encodeParams = true;/*** @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL.* 生成一個新的網址的模板。這是源于[[pattern]] ,并用于產生網址。*/private $_template;/*** @var string the regex for matching the route part. This is used in generating URL.* 該路線的部分匹配正則表達式。這是用來產生網址。*/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.* 路由的參數存儲數組*/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變成數組,并將器內容全部大寫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) {// 找到'://'之后的第一個'/'的位置,并截取之前的字符串作為host$this->host = substr($this->pattern, 0, $pos2);} else {$this->host = $this->pattern;}} else {$this->pattern = '/' . $this->pattern . '/';}/*** $rule的結構如下* [*     '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)) {// 匹配不帶正則表達式的路由配置,并放入_routeParams中存起來// 如上的例子中,$matches[1]=['id']foreach ($matches[1] as $name) {$this->_routeParams[$name] = "<$name>";}}$tr = ['.' => '\\.','*' => '\\*','$' => '\\$','[' => '\\[',']' => '\\]','(' => '\\(',')' => '\\)',];$tr2 = [];/*** 匹配帶正則表達式的路由配置* PREG_PATTERN_ORDER*   結果排序為$matches[0]保存完整模式的所有匹配, $matches[1] 保存第一個子組的所有匹配,以此類推。** PREG_SET_ORDER*   結果排序為$matches[0]包含第一次匹配得到的所有匹配(包含子組), $matches[1]是包含第二次匹配到的所有匹配(包含子組)的數組,以此類推。** PREG_OFFSET_CAPTURE*   如果這個標記被傳遞,每個發現的匹配返回時會增加它相對目標字符串的偏移量。*   注意這會改變matches中的每一個匹配結果字符串元素,使其成為一個第0個元素為匹配結果字符串,第1個元素為 匹配結果字符串在subject中的偏移量。** 如果沒有給定排序標記,假定設置為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+'// 如果正則表達式的匹配值為空,則默認為'[^\/]+'$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';}}

?

轉載于:https://www.cnblogs.com/taokai/p/5480005.html

總結

以上是生活随笔為你收集整理的yii2框架随笔29的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。