Zend Framework Framework routing Mechanism Code Analysis, zendframework_php tutorial

Source: Internet
Author: User
Tags zend framework

Zend Framework Framework routing Mechanism Code analysis, Zendframework


This paper analyzes the routing mechanism Code of Zend Framework framework. Share to everyone for your reference, as follows:

In the framework, the calling relationship for the route is:

1, Apache mod_rewrite module to the request routing to the framework of the startup script, is generally index.php;

2, front-end controller Zend_controller_front through the dispatch function for the request distribution;

3, router zend_controller_router_rewrite through the route function to handle the route, the router's existing routing rules, according to the Order of accession (similar to the stack, LIFO) on each route call match function, To check if the request matches the current routing rule, set the current route of the router ($_currentroute) to a matching route if it matches, and pass the parameter parsed by the route to the Zend_controller_request_http object. To complete the routing settings here.

If no route is found, the framework uses the index of the index controller as the action.

Analysis of function code in Zend_controller_router_route:

1. Constructor function

Public function __construct ($route, $defaults = Array (), $reqs = Array ()) {  $route = Trim ($route, $this->_urldelimi ter); Remove the URL delimiter for the end of the rule (default is/)  $this->_defaults = (array) $defaults;//default array, with variable name key  $this->_requirements = ( Array) $reqs; The variable needs to satisfy the regular expression, with the variable named key  if ($route! = ") {   foreach (Explode ($this->_urldelimiter, $route) as $pos = $part) {    //cut the rule into an array    if (substr ($part, 0, 1) = = $this->_urlvariable) {//If it is a variable definition     $name = substr ($part, 1); Gets the variable name     //If the variable defines a corresponding regular expression, gets the expression, otherwise null     $regex = (Isset ($reqs [$name])? $reqs [$name]: $this->_ Defaultregex);     The _parts array contains the parts of the rule, and if it is a variable, there is a name element in the array     $this->_parts[$pos] = Array (' name ' = = $name, ' regex ' = = $regex) ;     _vars contains the names of all variables in the rule     $this->_vars[] = $name;    } else {//ordinary string     $this->_parts[$pos] = Array (' Regex ' = ' $part);     if ($part! = ' * ') {      $this->_staticcount++;//The number of ordinary strings of the Rule}}}}}  

2. Matching algorithm

Public Function Match ($path) {$pathStaticCount = 0; $defaults = $this->_defaults; The default array, the key value of the array element is a copy of the variable name//default value array, but the value of the variable is all converted to a Boolean value, in fact, this value is not actually useful, the following program is only//by judging whether the key value exists and determine whether to include a variable, perhaps this is to save space,  But if so, why not use the $this->_defaults directly?  if (count ($defaults)) {$unique = Array_combine (Array_keys ($defaults), Array_fill (0, Count ($defaults), true));  } else {$unique = array (); } $path = Trim ($path, $this->_urldelimiter);   The incoming path is removed BaseURL, which ensures that the end-to-end delimiter is removed if ($path! = ") {$path = explode ($this->_urldelimiter, $path); foreach ($path as $pos + $pathPart) {if (!isset ($this->_parts[$pos)) {//The path is split into an array based on the URL delimiter, and the corresponding part of each section and rule     In comparison, if the path exists,//and the corresponding part does not exist in the rule, then the rule does not match, it is important to note that $pos is the corresponding part of the rule//and path.    return false; if ($this->_parts[$pos [' regex '] = = ' * ') {//If the current part of the rule is a wildcard *, the remainder of path is interpreted as a variable passed by the URL, and they follow the shape of the//variable name/variable value form pairs appear $parts = Array_slice ($path, $pos);     Get the remainder of path $this->_getwildcarddata ($parts, $unique);Break    } $part = $this->_parts[$pos]; $name = Isset ($part [' name '])?    $part [' name ']: null; $pathPart = UrlDecode ($pathPart);//Decodes the passed value if ($name = = = null) {//ordinary string, and the corresponding part of the rule compares equality if ($part [' regex ']! = $     Pathpart) {return false;      }} elseif ($part [' regex '] = = = NULL) {//If it is a variable but there is no regular expression that needs to be satisfied, then only the value is not empty the IF (strlen ($pathPart) = = 0) {     return false; }} else {//If a regular expression needs to be met for the variable, then validation $regex = $this->_regexdelimiter. '^' . $part [' regex ']. '$' . $this->_regexdelimiter.     ' IU ';     if (!preg_match ($regex, $pathPart)) {return false;     }} if ($name!== null) {//If it is a variable, set the value of the variable $this->_values[$name] = $pathPart; $unique [$name] = true;    There is no need to set it, this version does not use it at all} else {//The match count of the normal string is added 1, because the normal string in the rule must exist in path, otherwise the//match fails $pathStaticCount + +; }}}//$this->_values is the variable that is parsed, if ' * ' is present in the rule, then $this->_params is the//variable, otherwise an empty array, $this->_defaults is the default variable value provided by the rule Here, add the three arrays with ' + '/SoThe advantage is that if the subsequent array has the same non-integer key value as the previous array, the following will not overwrite the previous one, which is different from the Array_merge function, which is overwritten. That is, if the $this->_values already has a key controller//, then the controller element in the $this->_defaults is ignored, so that $this->_  The default value in defaults appears in the return value only if it does not exist in path//.  $return = $this->_values + $this->_params + $this->_defaults;   Check If all static mappings has been met if ($this->_staticcount! = $pathStaticCount) {//rule all normal strings must be matched in path  return false; }//After parsing, all variables defined by the rule must also appear, otherwise the foreach ($this->_vars as $var) {if (!array_key_exists ($var, $return)) {Retu   RN false; }} return $return;}

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article will help you to design PHP based on the Zend Framework framework.

Articles you may be interested in:

    • Zend Framework Framework Tutorial Zend_db_table_rowset Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table_row Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table Usage
    • Zend Framework Tutorial Zend_form component implement form submission and display Error prompt method
    • Introduction to Zend Framework Development Classic Tutorial
    • Zend Framework Smarty Extension Implementation method
    • Zend Framework implementation of basic functions of the message book (with demo source download)
    • The Zend framework implements the method of storing the session in Memcache
    • Zend Framework Paging Class usage
    • Zend Framework implements multi-file upload function instances
    • Zend Framework Introduction Environment configuration and the first Hello World example (with demo source download)
    • Zend Framework Tutorial to connect the database and perform additions and deletions of the method (attached to the demo source download)
    • Zend Framework Tutorial Zend_db_table Table Association Instance detailed

http://www.bkjia.com/PHPjc/1113719.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113719.html techarticle Zend Framework Framework routing Mechanism Code analysis, zendframework this article analyzes the ZEND framework framework routing mechanism code. Share to everyone for your reference, as follows: In the framework, there are ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.