当前位置: 首页 > 编程日记 > 正文

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

相关文章:

八种简易健康减肥瘦身法

①原地跑&#xff0c;紧实大腿肌肉&#xff1b;②上楼梯&#xff0c;瘦小腿、大腿、臀&#xff1b;③步行&#xff0c;瘦腿、腰&#xff1b;④瑜珈&#xff0c;瘦全身&#xff1b;⑤跳舞&#xff0c;瘦全身&#xff1b;⑥跳绳&#xff0c;瘦大腿、小腿&#xff1b;⑦晨操&#…

LaZagne检测windows本地存储的密码

LaZagne项目是用于检索存储在本地计算机上的大量密码的开源应用程序。 每个软件使用不同的技术&#xff08;明文&#xff0c;API&#xff0c;自定义算法&#xff0c;数据库等&#xff09;存储其密码。 该工具的开发是为了找到最常用的软件的密码。 下载到windows机器&#xff0…

通知提示SCPromptView

作者 陈小翰 关注 2017.03.24 18:01 字数 138 阅读 62评论 0喜欢 1SCPromptView SCPromptView : 显示在顶部的提示控件 你的star是我最大的动力 effect.gif安装 手动安装 下载源码&#xff0c;将SCPromptView文件夹拖进项目 CocoaPod pod SCPromptView 使用 SCPromptView 的用…

4566: [Haoi2016]找相同字符 SAM

折腾了好久。不过收获还是很多的。第一次自己去画SAM所建出来fail树。深入体会了这棵树的神奇性质。 当然&#xff0c;我最终靠着自己A掉了。&#xff08;这是我第一次推SAM的性质&#xff08;以前都是抄别人的&#xff0c;感觉自己好可耻&#xff09;&#xff0c;不过感觉好像…

NYOJ-232 How to eat more Banana

How to eat more Banana 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;4描述A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, …

UIView Animation

作者 嘿o大远 关注 2017.03.23 17:02* 字数 402 阅读 47评论 1喜欢 3今天总结一下UIView动画就是 UiView动画是基于高层API封装进行封装的,对UIView的属性进行修改时候所产生的动画. 基本动画 下面两种方法是最常用的两种. (void)animateWithDuration:(NSTimeInterval)duratio…

[转]Ext Grid控件的配置与方法

http://www.blogjava.net/wangdetian168/archive/2011/04/12/348651.html 1、Ext.grid.GridPanel 主要配置项&#xff1a; store&#xff1a;表格的数据集 columns&#xff1a;表格列模式的配置数组&#xff0c;可自动创建ColumnModel列模式 autoExpandColumn&#xff1a;自动充…

永久设置SecureCRT的背景色和文字颜色方案

对于默认的连接颜色感觉不舒服&#xff0c;一通乱搞&#xff0c;总结出这些。 一、对于临时设置&#xff0c;可以如下操作&#xff1a; 首先options -- session - appearance 此处可以设置临时的窗口背景&#xff0c;字体颜色&#xff0c;大小等等&#xff0c;为什么说是临时&a…

利用Procdump+Mimikatz获取Windows帐户密码

0x01 前言&#xff1a; 前段时间拿下一个网站的shell&#xff0c;很幸运的是直接就是System权限&#xff0c;结果发现执行添加用户命令并不能成功回显 看了下系统进程&#xff0c;原来是开启了360的主动防御&#xff0c;奈何也不会做免杀&#xff0c;上传exp运行就被杀&#x…

一个逻辑清晰的购物车模型

效果图 2017-03-25 18.28.23.gifGitHub: https://github.com/lll1024/JVShopcart 说明 这是一个具备常规功能并方便改造的购物车模型 一共包含五个模块&#xff1a; JVShopcartViewController: 购物车控制器 负责协调Model和View 只有100多行代码JVShopcartFormat: 负责网络请求…

Nginx基本配置、性能优化指南

转载自&#xff1a;http://www.chinaz.com/web/2015/0424/401323.shtml 大多数的Nginx安装指南告诉你如下基础知识——通过apt-get&#xff0c;或yum安装&#xff0c;修改这里或那里的几行配置&#xff0c;好了&#xff0c;你已经有了一个Web服务器了&#xff01;而且&#xff…

关于批量修改AD域用户的脚本

最近几天帮人弄了个脚本&#xff0c;是修改域用户属性的脚本&#xff0c;今天看到徐火军写的 关于批量修改用户属性 脚本&#xff0c;觉得有必要把我的成果分享给大家。什么都不说了&#xff0c;上脚本&#xff1a; Dim oFSO, oTF, iDim sLineDim sLoginName 用户批量文件Const…

Python multiprocess 多进程模块

转发&#xff1a;http://www.langzi.fun/Python multiprocess 多进程模块.html 需要注意的是&#xff0c;如果使用多线程&#xff0c;用法一定要加上if __name____main__:(Python中的multiprocess提供了Process类&#xff0c;实现进程相关的功能。但是它基于fork机制&#xff…

PHP正则数组

<?php //正则表达式//斜杠代表定界符 /^$///$str "好厉害18653378660了hi请勿嫁得好15165339515安徽dah矮冬瓜 拍行业大概啊好广东也欺负偶怕哈";//$reg "/(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}/";//echo preg_repl…

iOS Socket Client 通讯

iOS Socket Client 通讯 阅读 239收藏 192017-03-29原文链接&#xff1a;https://github.com/guangzhouxia/JTSocketiOS Socket Client 通讯&#xff08;偏流程和代码展示&#xff09;&#xff0c;具体原理可以在网上搜索到很多&#xff0c;就不多做追叙复制了。。。 —— 由广…

api工程IOS学习:在IOS开发中使用GoogleMaps SDK

今天一直在学习api工程之类的问题,今天正好有机会和大家分享一下. 官方文档地址&#xff1a;https://developers.google.com/maps/documentation/ios/start#getting_the_google_maps_sdk_for_ios 一、申请一个收费的API KEY 要应用GoogleMaps SDK&#xff0c;必须要为你的应用申…

Python多进程与进程锁的基本使用

Python的multiprocessing模块提供了多种进程间通信的方式&#xff0c;如Queue、Pipe等。 Queue是multiprocessing提供的一个模块&#xff0c;它的数据结构就是"FIFO——first in first out"的队列&#xff0c;常用的方法有&#xff1a;put(object)入队&#xff1b;g…

[容易]比较字符串

题目来源&#xff1a;http://www.lintcode.com/zh-cn/problem/compare-strings/ 先贴上错误代码&#xff0c;测试案例没有全部通过。不一定是顺序的。 1 class Solution {2 public:3 /**4 * param A: A string includes Upper Case letters5 * param B: A string…

iOS 一行命令发布 Pod 框架

作者 ripperhe 关注 2017.03.30 23:38* 字数 5589 阅读 27评论 0喜欢 2前言 目前比较流行的组件化开发&#xff0c;针对多个 app 要用同一套代码&#xff0c;将其做成 pod 仓库是比较好的解决方案。代码只有一份放在组件仓库&#xff0c;需要集成的 app 只需要将其 pod 到工程内…

[bbk4966]第70集 第8章 -性能维护 01

本章前言: 每秒钟&#xff0c;产生的日志文件多少&#xff0c;如果产生很多的redo log 信息&#xff0c;说明负荷量大差生的原因是DML操作太多. 假如oracle database 属于dedicate server&#xff0c;使用top session方式排查数据库性能问题&#xff0c;是比较适合的.根据SESSI…

Python中正则匹配与中文的问题

笔者改写了一个爬虫&#xff0c;来爬取补天SRC的漏洞认领页面&#xff0c;将单位名称、漏洞名称、漏洞危害等级爬取下来&#xff0c;但是在正则匹配"漏洞名称"的过程中遇到了一些麻烦。 如上图&#xff0c;想要把"SQL注入漏洞"字符串正则匹配出来&#xf…

项目/程序的流程走向

领导用户需求前景准备分析(OOA)设计(OOD)实现(OOP)测试部署发布跟踪维护升级新田月会|新细胞|君宁天下|htttp://www.xintianyuehui.cn 作者&#xff1a;宁骑 联系QQ&#xff1a;1075858260转载于:https://www.cnblogs.com/ncellit/p/5491828.html

使用 UIBezierPath 进行简单的图形绘制

这篇文章介绍UIBezierPath的详细的使用, 以及一些细节! 创建一个XTBezierPath继承于UIView的类 使用drawRect 完成图形的绘制 在drawRect方法完成绘制 使用 moveToPoint, addLineToPoint两个方法绘制一个任意多边形 其中w, h 代表自定义View的宽, 高 代码如下: // 初始化一个UI…

利用python实现IP扫描

需求&#xff1a;写一个脚本&#xff0c;判断192.168.11.0/24网络里&#xff0c;当前在线ip有哪些&#xff1f; 知识点: 1 使用subprocess模块&#xff0c;来调用系统命令&#xff0c;执行ping 192.168.11.xxx 命令 2 调用系统命令执行ping命令的时候&#xff0c;会有返回值…

EFQRCode:自动生成花式二维码

原文链接&#xff1a;https://github.com/EyreFree/EFQRCodeEFQRCode&#xff1a;自动生成花式二维码。# 为开源点赞# —— 由SwiftLanguage分享EFQRCode is a tool to generate QRCode UIImage or recognize QRCode from UIImage, in Swift. It is based on CIDetector and CI…

centos删除系统自带的httpd

centos删除系统自带的httpd 1、[rootlocalhost etc]# rpm -qa|grep httpd&#xff0c;查看与httpd相关软件包。 httpd-tools-2.2.15-15.el6.centos.i686 httpd-2.2.15-15.el6.centos.i686 www.2cto.com 2、然后删除httpd&#xff1a; [rootlocalhost etc]# rpm -e httpd 出现问…

[C#]ASP.NET MVC 3 在线学习资料

最近在研究如何把Twitter Bootstrap移植到ASP.NET MVC 3上&#xff0c;攒了点资料&#xff0c;先贴在之里&#xff0c;以后整理了写心得。 1. http://www.codeproject.com/Articles/404633/Transform-ASP-NET-MVC3-Default-Template-with-Twitt 这是一篇介绍如何把默认的ASP.NE…

域渗透提权之MS14-068

0x00 前言 在做渗透测试时&#xff0c;当遇到域环境&#xff0c;获取到一个域成员账号后&#xff0c;如果域控制器未打好补丁&#xff0c;则可以利用本文所提到的漏洞&#xff0c;快速获取到域控制器权限。笔者这里总结网上已有资料&#xff0c;加以描述&#xff0c;希望你能在…

iOS 高可控性日历基础组件 - SKCalendarView 的使用和实现思路的分享

阅读 61收藏 52017-04-02原文链接&#xff1a;http://www.jianshu.com/p/ce4c64a4d437SKCalendarView 是一个高可控性的日历基础组件&#xff0c;为了提高应用的自由度&#xff0c;默认只提供了日历部分的视图封装&#xff0c;但不涵盖切换月份按钮、年月分显示等非关键性控件&…