Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of routing Mechanism in Zend Framework Framework

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you an example analysis of the routing mechanism of the Zend Framework framework, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The details are as follows:

In the framework, the invocation relationships for routing are:

1. Apache's mod_rewrite module routes the request to the framework's startup script, usually index.php

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

3. Router Zend_Controller_Router_Rewrite handles 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 joining (similar to stack, LIFO) to check whether the request matches the current routing rule. If so, set the router's current currentRoute variable ($_ route) to the matching route. The parameters parsed by route are passed to the Zend_Controller_Request_Http object, and the routing settings are completed here.

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

Analyze the function code in Zend_Controller_Router_Route:

1. Constructor

Public function _ _ construct ($route, $defaults = array (), $reqs = array ()) {$route = trim ($route, $this- > _ urlDelimiter); / / remove the url delimiter at the beginning and end of the rule (default is /) $this- > _ defaults = (array) $defaults; / default array, with the variable name as key $this- > _ requirements = (array) $reqs / / the regular expression that the variable needs to satisfy, with the variable name as 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) / / get the variable name / / if the variable defines a corresponding regular expression, get the expression, otherwise set it to null $regex = (isset ($reqs [$name])? $reqs [$name]: $this- > _ defaultRegex); / / _ parts array contains all 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 variables in the rule $this- > _ vars [] = $name;} else {/ / normal string $this- > _ parts [$pos] = array ('regex' = > $part); if ($part! =' *') {$this- > _ staticCount++; / / the number of ordinary strings in the rule}

2. Matching algorithm

Public function match ($path) {$pathStaticCount = 0; $defaults = $this- > _ defaults / / default value array, the key value of the array element is a copy of the variable name / / default array, but the value of the variable is all changed to a Boolean value, in fact, this value is of no practical use. The following program only / / determines whether a variable is included by judging whether the key value exists, which may be done to save space, but in that case / why not just use $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); / / baseUrl has been removed. Make sure the delimiter if ($path! =') {$path = explode ($this- > _ urlDelimiter, $path) is removed. Foreach ($path as $pos = > $pathPart) {if (! isset ($this- > _ parts [$pos])) {/ / divide the path into an array according to the url separator, and compare each part with the corresponding part of the rule. If there is a corresponding part in the path, / / and there is no corresponding part in the rule, then the rule must not match. Note here that $pos corresponds to the rule / / and the corresponding part of path through it. Return false;} if ($this- > _ parts [$pos] ['regex'] = =' *') {/ / if the current part of the rule is a wildcard *, the rest of path is interpreted as a variable passed by url, and they appear in pairs of $parts = array_slice ($path, $pos) in the form of / / "variable name / variable value". / / get the rest of the path $this- > _ getWildcardData ($parts, $unique); break;} $part = $this- > _ parts [$pos]; $name = isset ($part ['name'])? $part [' name']: null; $pathPart = urldecode ($pathPart) / / A pair of passed values are decoded by if ($name = null) {/ / ordinary string. Compare with the corresponding part of the rule for 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 if (strlen ($pathPart) = = 0) {return false;}} else {/ / if a regular expression needs to be satisfied for the variable, then validate $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} else {/ / to add 1 to the match count of ordinary strings, because the ordinary strings in the rule must exist in path, otherwise it is / / matching failure $pathStaticCount++ } / / $this- > _ values stores the variables obtained by the analysis. If there is a'*'in the rule, $this- > _ params is the acquired / / variable, otherwise it is an empty array, and $this- > _ defaults is the default variable value provided by the rule. The advantage of adding the three arrays with'+'is that if the latter array has the same non-integer key value as the previous array The latter will not overwrite the previous one, which / / is different from the array_merge function, which will overwrite. That is, if the key controller / / already exists in $this- > _ values, the controller element in $this- > _ defaults is ignored, so that the default value in $this- > _ 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) {/ / all ordinary strings of the rule must match return false in path } / / after parsing, all variables defined by the rule must also appear, otherwise it will be regarded as mismatching foreach ($this- > _ vars as $var) {if (! array_key_exists ($var, $return)) {return false;}} return $return;} all the contents of the article "sample Analysis of Zend Framework Framework routing Mechanism". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report