Table of Contents
Zend Framework框架路由机制代码分析,zendframework
您可能感兴趣的文章:
Home php教程 php手册 Zend Framework框架路由机制代码分析,zendframework

Zend Framework框架路由机制代码分析,zendframework

Jun 13, 2016 am 08:43 AM
framework php zend

Zend Framework框架路由机制代码分析,zendframework

本文分析了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++; //该规则的普通字符串的个数
     }
    }
   }
  }
}

Copy after login

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;
}

Copy after login

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Zend Framework框架教程之Zend_Db_Table_Rowset用法实例分析
  • Zend Framework教程之Zend_Db_Table_Row用法实例分析
  • Zend Framework教程之Zend_Db_Table用法详解
  • Zend Framework教程之Zend_Form组件实现表单提交并显示错误提示的方法
  • Zend Framework开发入门经典教程
  • Zend Framework框架Smarty扩展实现方法
  • Zend Framework实现具有基本功能的留言本(附demo源码下载)
  • Zend Framework实现将session存储在memcache中的方法
  • Zend Framework分页类用法详解
  • Zend Framework实现多文件上传功能实例
  • Zend Framework入门之环境配置及第一个Hello World示例(附demo源码下载)
  • Zend Framework教程之连接数据库并执行增删查的方法(附demo源码下载)
  • Zend Framework教程之Zend_Db_Table表关联实例详解
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles