国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Zend Framework routing mechanism code analysis, zendframework
Articles you may be interested in:
Home Backend Development PHP Tutorial Zend Framework routing mechanism code analysis, zendframework_PHP tutorial

Zend Framework routing mechanism code analysis, zendframework_PHP tutorial

Jul 12, 2016 am 08:56 AM
framework php zend

Zend Framework routing mechanism code analysis, zendframework

This article analyzes the Zend Framework routing mechanism code. Share it with everyone for your reference, the details are as follows:

In the framework, the calling relationship of routing is:

1. Apache’s mod_rewrite module routes requests to the framework’s startup script, usually index.php;

2. The front-end controller Zend_Controller_Front distributes requests through the dispatch function;

3. The router Zend_Controller_Router_Rewrite processes routing through the route function. For the existing routing rules in the router, the match function is called on each route in the reverse order of the adding order (similar to a stack, last in first out) to check whether the request is consistent with the current one. The routing rules match. If they match, set the router's current route variable ($_currentRoute) to the matching route, and pass the parameters parsed by route to the Zend_Controller_Request_Http object. Complete the route setting here.

If no route is found, the framework will use the index action of the Index controller.

Analysis of function code in Zend_Controller_Router_Route:

1. Constructor

public function __construct($route, $defaults = array(), $reqs = array())
{
  $route = trim($route, $this->_urlDelimiter); //去掉規(guī)則首尾的url分隔符(默認(rèn)是/)
  $this->_defaults = (array) $defaults; //默認(rèn)值數(shù)組,以變量名為鍵
  $this->_requirements = (array) $reqs; //變量需要滿足的正則表達(dá)式,以變量名為鍵
  if ($route != '') {
   foreach (explode($this->_urlDelimiter, $route) as $pos => $part) {
    //把規(guī)則切分為一個數(shù)組
    if (substr($part, 0, 1) == $this->_urlVariable) {//如果是一個變量的定義
     $name = substr($part, 1); //獲取變量名
     //如果該變量定義了對應(yīng)的正則表達(dá)式,則獲取該表達(dá)式,否則置為null
     $regex = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex);
     //_parts數(shù)組包含了規(guī)則的各個部分,如果是變量的話,數(shù)組中有name元素
     $this->_parts[$pos] = array('name' => $name, 'regex' => $regex);
     //_vars包含了該規(guī)則中的所有變量的名字
     $this->_vars[] = $name;
    } else { //普通字符串
     $this->_parts[$pos] = array('regex' => $part);
     if ($part != '*') {
      $this->_staticCount++; //該規(guī)則的普通字符串的個數(shù)
     }
    }
   }
  }
}

2. Matching algorithm

public function match($path)
{
  $pathStaticCount = 0;
  $defaults = $this->_defaults; //默認(rèn)值數(shù)組,數(shù)組元素的鍵值是變量名
   //默認(rèn)值數(shù)組的一個拷貝,不過變量的值全部換成布爾值,其實這個值并沒有實際用處,下面程序僅僅
  //是通過判斷鍵值是否存在而確定是否包含一個變量,可能這么做是為了節(jié)省空間,不過要是這樣的話
  //不如直接使用 $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是已經(jīng)去掉baseUrl的,這里確保去掉首尾的分隔符
  if ($path != '') {
   $path = explode($this->_urlDelimiter, $path);
   foreach ($path as $pos => $pathPart) {
    if (!isset($this->_parts[$pos])) {
     //把path根據(jù)url分隔符分割為數(shù)組后,把每一部分和規(guī)則的對應(yīng)部分比較,如果path中存在,
     //而規(guī)則中不存在對應(yīng)部分,那么該規(guī)則肯定不匹配,這里要注意$pos,是通過它把規(guī)則
     //和path的對應(yīng)部分對應(yīng)起來。
     return false;
    }
    if ($this->_parts[$pos]['regex'] == '*') {
      //如果規(guī)則的當(dāng)前部分是通配符*,則把path的剩余部分解釋為url傳遞過來的變量,他們按照
     //“變量名/變量值”這樣的形式成對出現(xiàn)
     $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);//對傳過來的值進(jìn)行解碼
    if ($name === null) {//普通字符串,和規(guī)則的對應(yīng)部分比較是否相等即可
     if ($part['regex'] != $pathPart) {
      return false;
     }
    } elseif ($part['regex'] === null) {
      //如果是變量,但是沒有需要滿足的正則表達(dá)式,那么只有值不為空就可以了
     if (strlen($pathPart) == 0) {
      return false;
     }
    } else {//如果對該變量需要滿足一個正則表達(dá)式,那么這里進(jìn)行驗證
     $regex = $this->_regexDelimiter . '^' . $part['regex'] . '$' . $this->_regexDelimiter . 'iu';
     if (!preg_match($regex, $pathPart)) {
      return false;
     }
    }
    if ($name !== null) {
     // 如果是一個變量,則設(shè)置變量的值
     $this->_values[$name] = $pathPart;
     $unique[$name] = true; //其實沒有必要設(shè)置,這個版本根本就沒有用它
    } else {
     //把普通字符串的匹配計數(shù)加1,因為規(guī)則中的普通字符串是必須在path中存在的,否則就是
     //匹配失敗
     $pathStaticCount++;
    }
   }
  }
   //$this->_values中保存的是分析獲取的變量,如果規(guī)則中存在‘*',則$this->_params是獲取的
  //變量,否則是空數(shù)組,$this->_defaults是規(guī)則提供的默認(rèn)變量值,這里用‘+'把三個數(shù)組相加
  //這樣的好處是如果后面的數(shù)組與前面的數(shù)組有相同的非整數(shù)的鍵值,后面的不會覆蓋前面的,這
  //與array_merge函數(shù)有區(qū)別,后者是會覆蓋的。也就是說,如果$this->_values 中已經(jīng)有鍵controller
  //,那么$this->_defaults中的controller元素就被忽略,這樣就$this->_defaults中的默認(rèn)值只有在path
  //中不存在的時候才會出現(xiàn)在返回值中。
  $return = $this->_values + $this->_params + $this->_defaults;
  // Check if all static mappings have been met
  if ($this->_staticCount != $pathStaticCount) {//規(guī)則的所有普通字符串必須在path中得到匹配
   return false;
  }
  // 解析完后,規(guī)則定義的所有變量也必須全部出現(xiàn),否則視為不匹配
  foreach ($this->_vars as $var) {
   if (!array_key_exists($var, $return)) {
    return false;
   }
  }
  return $return;
}

Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

Articles you may be interested in:

  • Zend Framework Tutorial - Zend_Db_Table_Rowset Usage Example Analysis
  • Zend Framework Tutorial - Zend_Db_Table_Row Usage Example Analysis
  • Zend Framework Tutorial: Detailed explanation of Zend_Db_Table usage
  • Zend Framework tutorial: Zend_Form component to implement form submission and display error prompts
  • Zend Framework development introduction classic tutorial
  • Zend Framework Smarty extension implementation Method
  • Zend Framework implements a guestbook with basic functions (with demo source code download)
  • Zend Framework implements method of storing session in memcache
  • Zend Framework paging class usage Detailed explanation
  • Zend Framework implements multi-file upload function example
  • Environment configuration for getting started with Zend Framework and the first Hello World example (with demo source code download)
  • Zend Framework tutorial How to connect to the database and perform add/delete queries (with demo source code download)
  • Detailed explanation of the Zend_Db_Table table association example in the Zend Framework tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113719.htmlTechArticleZend Framework routing mechanism code analysis, zendframework This article analyzes the Zend Framework routing mechanism code. Share it with everyone for your reference, the details are as follows: In the framework, there are...
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 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)

How do I stay up-to-date with the latest PHP developments and best practices? How do I stay up-to-date with the latest PHP developments and best practices? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

What is PHP, and why is it used for web development? What is PHP, and why is it used for web development? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

How to set PHP time zone? How to set PHP time zone? Jun 25, 2025 am 01:00 AM

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

How do I validate user input in PHP to ensure it meets certain criteria? How do I validate user input in PHP to ensure it meets certain criteria? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

What is data serialization in PHP (serialize(), unserialize())? What is data serialization in PHP (serialize(), unserialize())? Jun 22, 2025 am 01:03 AM

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

How do I embed PHP code in an HTML file? How do I embed PHP code in an HTML file? Jun 22, 2025 am 01:00 AM

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

What are the best practices for writing clean and maintainable PHP code? What are the best practices for writing clean and maintainable PHP code? Jun 24, 2025 am 12:53 AM

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

How do I execute SQL queries using PHP? How do I execute SQL queries using PHP? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles