Code Analysis _php Instance of Zend framework frame routing mechanism

Source: Internet
Author: User
Tags explode zend zend framework

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

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

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

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

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

If no route is found, the framework uses the Index controller's index action.

Analysis of the function code in Zend_controller_router_route:

1. Constructor function

The Public function __construct ($route, $defaults = Array (), $reqs = Array ()) {$route = Trim ($route, $this->_urldelimit ER); Remove the URL delimiter from the end of the rule (default is/) $this->_defaults = (array) $defaults; An array of default values, with the variable name key $this->_requirements = (array) $reqs;
    A regular expression that the variable needs to satisfy, with the variable named key if ($route!= ') {foreach (Explode ($this->_urldelimiter, $route) as $pos => $part) { 
     Divide the rule into an array if (substr ($part, 0, 1) = = $this->_urlvariable) {//If the definition of a variable $name = substr ($part, 1);//Get variable name
     Gets the expression if the variable defines the corresponding regular expression, otherwise null $regex = (Isset ($reqs [$name])? $reqs [$name]: $this->_defaultregex); 
     The _parts array contains parts of the rule, and if it is a variable, the array has the name element $this->_parts[$pos] = Array (' name ' => $name, ' regex ' => $regex);
    _vars contains the names of all the variables in the rule $this->_vars[] = $name;
     else {//normal string $this->_parts[$pos] = array (' regex ' => $part);

 if ($part!= ' * ') {$this->_staticcount++;//number of normal strings for this rule}}}

2, matching algorithm

The Public Function match ($path) {$pathStaticCount = 0; $defaults = $this->_defaults; The default value array, the key value of the array element is a copy of the variable name//default value array. However, the value of the variable is all converted to a Boolean value, in fact, this value is not practical use, the following program only//is determined by the existence of the key to determine whether the inclusion of a variable, may do so in order to save space,
  But what if it's better to use $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 has been removed from the BaseURL, where it is ensured that the end to end separator is removed if ($path!= ') {$path = explode ($this->_urldelimiter, $path); foreach ($path as $pos => $pathPart) {if (!isset ($this->_parts[$pos)) {//to divide the path by the URL delimiter according to each part and the rule's corresponding
     Part of the comparison, if there is in path,//And there is no corresponding part in the rule, then the rule does not match, and here you should pay attention to $pos, by which you correspond the rule//to the corresponding part of the path.
    return false; if ($this->_parts[$pos] [' regex '] = = ' * ') {//If the current part of the rule is a wildcard *, interpret the remainder of the path as the variable passed by the URL, and they follow//"variable name/variable value" A kind of form into pairs appears $parts = Array_slice ($path, $pos); Gets the remainder of path $this->_getwildcardData ($parts, $unique);
    Break
    } $part = $this->_parts[$pos]; $name = Isset ($part [' name '])?
    $part [' name ']: null;  $pathPart = UrlDecode ($pathPart);//To decode the passed value if ($name = = null) {//normal string, and compare the corresponding parts of the rule to be equal if ($part [' regex ']!=
     $pathPart) {return false;
      } elseif ($part [' regex '] = = null) {//If it is a variable, but there is no regular expression to satisfy, then only the value is not empty if (strlen ($pathPart) = = 0) {
     return false; } else {//If a regular expression needs to be satisfied with the variable, verify here $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; 
    In fact, there is no need to set, this version does not use it at all} else {//the normal string matching count plus 1, because the regular string in the rule must exist in path, otherwise it is//match failure $pathStaticCount + +; }}//$this->_values is an analysis-acquired variable, and if there is a ' * ' in the rule, $this->_params is acquired//variables, otherwise it is an empty array, $this->_defaults is the default variable value provided by the rule, and the advantage of adding three arrays with ' + ' is that if the following array has the same non integer key value as the preceding array, the following will not overwrite the previous one, this//with Array_ The merge function is different, and the latter is overwritten. In other words, if there is already a key controller//In the $this->_values, then the controller element in $this->_defaults is ignored, so $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 have been met if ($this->_staticcount!= $pathStaticCount) {//rule all normal strings must be matched in path
  return false;
    All the variables defined by the rule must also appear, or they will not be treated as mismatched foreach ($this->_vars as $var) {if (!array_key_exists ($var, $return)) {
   return false;
} return $return;

 }

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

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.