Zend Framework框架路由机制代码分析_PHP
本文分析了Zend Framework框架路由机制代码。分享给大家供大家参考,具体如下:
在框架中,有关路由的调用关系为:
1、apache的mod_rewrite模块把请求路由到框架的启动脚本,一般是index.php;
2、前端控制器Zend_Controller_Front通过dispatch函数进行请求分发;
3、路由器Zend_Controller_Router_Rewrite通过route函数处理路由,对路由器中已有的路由规则,按照加入顺序的逆序(类似于栈,后进先出)对每个route调用match函数,以检查请求是否和当前路由规则匹配,如果匹配的话把路由器的当前路由这个变量($_currentRoute)设置为匹配的路由,并把route解析出来的参数传给Zend_Controller_Request_Http对象,到这里完成路由设置。
如果没有发现路由,框架会使用Index控制器的index这个action。
对Zend_Controller_Router_Route中的函数代码分析:
1、构造函数
public function __construct($route, $defaults = array(), $reqs = array()) { $route = trim($route, $this->_urlDelimiter); //去掉规则首尾的url分隔符(默认是/) $this->_defaults = (array) $defaults; //默认值数组,以变量名为键 $this->_requirements = (array) $reqs; //变量需要满足的正则表达式,以变量名为键 if ($route != '') { foreach (explode($this->_urlDelimiter, $route) as $pos => $part) { //把规则切分为一个数组 if (substr($part, 0, 1) == $this->_urlVariable) {//如果是一个变量的定义 $name = substr($part, 1); //获取变量名 //如果该变量定义了对应的正则表达式,则获取该表达式,否则置为null $regex = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex); //_parts数组包含了规则的各个部分,如果是变量的话,数组中有name元素 $this->_parts[$pos] = array('name' => $name, 'regex' => $regex); //_vars包含了该规则中的所有变量的名字 $this->_vars[] = $name; } else { //普通字符串 $this->_parts[$pos] = array('regex' => $part); if ($part != '*') { $this->_staticCount++; //该规则的普通字符串的个数 } } } } }
2、匹配算法
public function match($path) { $pathStaticCount = 0; $defaults = $this->_defaults; //默认值数组,数组元素的键值是变量名 //默认值数组的一个拷贝,不过变量的值全部换成布尔值,其实这个值并没有实际用处,下面程序仅仅 //是通过判断键值是否存在而确定是否包含一个变量,可能这么做是为了节省空间,不过要是这样的话 //不如直接使用 $this->_defaults了? if (count($defaults)) { $unique = array_combine(array_keys($defaults), array_fill(0, count($defaults), true)); } else { $unique = array(); } $path = trim($path, $this->_urlDelimiter); //传入的path是已经去掉baseUrl的,这里确保去掉首尾的分隔符 if ($path != '') { $path = explode($this->_urlDelimiter, $path); foreach ($path as $pos => $pathPart) { if (!isset($this->_parts[$pos])) { //把path根据url分隔符分割为数组后,把每一部分和规则的对应部分比较,如果path中存在, //而规则中不存在对应部分,那么该规则肯定不匹配,这里要注意$pos,是通过它把规则 //和path的对应部分对应起来。 return false; } if ($this->_parts[$pos]['regex'] == '*') { //如果规则的当前部分是通配符*,则把path的剩余部分解释为url传递过来的变量,他们按照 //“变量名/变量值”这样的形式成对出现 $parts = array_slice($path, $pos); //获取path的剩余部分 $this->_getWildcardData($parts, $unique); break; } $part = $this->_parts[$pos]; $name = isset($part['name']) ? $part['name'] : null; $pathPart = urldecode($pathPart);//对传过来的值进行解码 if ($name === null) {//普通字符串,和规则的对应部分比较是否相等即可 if ($part['regex'] != $pathPart) { return false; } } elseif ($part['regex'] === null) { //如果是变量,但是没有需要满足的正则表达式,那么只有值不为空就可以了 if (strlen($pathPart) == 0) { return false; } } else {//如果对该变量需要满足一个正则表达式,那么这里进行验证 $regex = $this->_regexDelimiter . '^' . $part['regex'] . '$' . $this->_regexDelimiter . 'iu'; if (!preg_match($regex, $pathPart)) { return false; } } if ($name !== null) { // 如果是一个变量,则设置变量的值 $this->_values[$name] = $pathPart; $unique[$name] = true; //其实没有必要设置,这个版本根本就没有用它 } else { //把普通字符串的匹配计数加1,因为规则中的普通字符串是必须在path中存在的,否则就是 //匹配失败 $pathStaticCount++; } } } //$this->_values中保存的是分析获取的变量,如果规则中存在‘*',则$this->_params是获取的 //变量,否则是空数组,$this->_defaults是规则提供的默认变量值,这里用‘+'把三个数组相加 //这样的好处是如果后面的数组与前面的数组有相同的非整数的键值,后面的不会覆盖前面的,这 //与array_merge函数有区别,后者是会覆盖的。也就是说,如果$this->_values 中已经有键controller //,那么$this->_defaults中的controller元素就被忽略,这样就$this->_defaults中的默认值只有在path //中不存在的时候才会出现在返回值中。 $return = $this->_values + $this->_params + $this->_defaults; // Check if all static mappings have been met if ($this->_staticCount != $pathStaticCount) {//规则的所有普通字符串必须在path中得到匹配 return false; } // 解析完后,规则定义的所有变量也必须全部出现,否则视为不匹配 foreach ($this->_vars as $var) { if (!array_key_exists($var, $return)) { return false; } } return $return; }
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

According to news on December 9, Cooler Master recently demonstrated a mini chassis kit in cooperation with notebook modular solution provider Framework at a demonstration event at the Taipei Compute Show. The unique thing about this kit is that it can be compatible with and Install the motherboard from the framework notebook. Currently, this product has begun to be sold on the market, priced at 39 US dollars, which is equivalent to approximately 279 yuan at the current exchange rate. The model number of this chassis kit is named "frameWORKMAINBOARDCASE". In terms of design, it embodies the ultimate compactness and practicality, measuring only 297x133x15 mm. Its original design is to be able to seamlessly connect to framework notebooks
