From a095d383e2736d210cea32c4d35f5f3a7c9b03ee Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 27 Feb 2013 11:19:24 -0500 Subject: [PATCH] refactored url management. --- framework/web/Application.php | 2 +- framework/web/UrlManager.php | 2 +- framework/web/UrlRule.php | 15 ++++++++------- tests/unit/framework/web/UrlRuleTest.php | 12 +++++++----- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/framework/web/Application.php b/framework/web/Application.php index 2159878..a525c54 100644 --- a/framework/web/Application.php +++ b/framework/web/Application.php @@ -32,7 +32,7 @@ class Application extends \yii\base\Application */ public function processRequest() { - $route = $this->getUrlManager()->parseUrl($this->getRequest()); + $route = $this->getUrlManager()->parseRequest($this->getRequest()); return $this->runAction($route, $_GET); } diff --git a/framework/web/UrlManager.php b/framework/web/UrlManager.php index bf52f82..8849d7e 100644 --- a/framework/web/UrlManager.php +++ b/framework/web/UrlManager.php @@ -119,7 +119,7 @@ class UrlManager extends Component $pathInfo = $request->pathInfo; /** @var $rule UrlRule */ foreach ($this->rules as $rule) { - if (($result = $rule->parseUrl($this, $pathInfo)) !== false) { + if (($result = $rule->parseRequest($this, $request)) !== false) { return $result; } } diff --git a/framework/web/UrlRule.php b/framework/web/UrlRule.php index 081a0ee..da4abc0 100644 --- a/framework/web/UrlRule.php +++ b/framework/web/UrlRule.php @@ -57,10 +57,10 @@ class UrlRule extends Object */ public $verb; /** - * @var integer a value indicating if this rule should be used for both URL parsing and creation, + * @var integer a value indicating if this rule should be used for both request parsing and URL creation, * parsing only, or creation only. - * If not set, it means the rule is both URL parsing and creation. - * If it is [[PARSING_ONLY]], the rule is for URL parsing only. + * If not set or 0, it means the rule is both request parsing and URL creation. + * If it is [[PARSING_ONLY]], the rule is for request parsing only. * If it is [[CREATION_ONLY]], the rule is for URL creation only. */ public $mode; @@ -152,22 +152,23 @@ class UrlRule extends Object } /** - * Parses the given path info and returns the corresponding route and parameters. + * Parses the given request and returns the corresponding route and parameters. * @param UrlManager $manager the URL manager - * @param string $pathInfo the path info to be parsed. It should not have slashes at the beginning or the end. + * @param Request $request the request component * @return array|boolean the parsing result. The route and the parameters are returned as an array. * If false, it means this rule cannot be used to parse this path info. */ - public function parseUrl($manager, $pathInfo) + public function parseRequest($manager, $request) { if ($this->mode === self::CREATION_ONLY) { return false; } - if ($this->verb !== null && !in_array(\Yii::$app->getRequest()->verb, $this->verb, true)) { + if ($this->verb !== null && !in_array($request->verb, $this->verb, true)) { return false; } + $pathInfo = $request->pathInfo; $suffix = (string)($this->suffix === null ? $manager->suffix : $this->suffix); if ($suffix !== '' && $pathInfo !== '') { $n = strlen($suffix); diff --git a/tests/unit/framework/web/UrlRuleTest.php b/tests/unit/framework/web/UrlRuleTest.php index fd9a8bd..8b2b578 100644 --- a/tests/unit/framework/web/UrlRuleTest.php +++ b/tests/unit/framework/web/UrlRuleTest.php @@ -4,6 +4,7 @@ namespace yiiunit\framework\web; use yii\web\UrlManager; use yii\web\UrlRule; +use yii\web\Request; class UrlRuleTest extends \yiiunit\TestCase { @@ -22,18 +23,19 @@ class UrlRuleTest extends \yiiunit\TestCase } } - public function testParseUrl() + public function testParseRequest() { $manager = new UrlManager; - $suites = $this->getTestsForParseUrl(); + $request = new Request; + $suites = $this->getTestsForParseRequest(); foreach ($suites as $i => $suite) { list ($name, $config, $tests) = $suite; $rule = new UrlRule($config); foreach ($tests as $j => $test) { - $pathInfo = $test[0]; + $request->pathInfo = $test[0]; $route = $test[1]; $params = isset($test[2]) ? $test[2] : array(); - $result = $rule->parseUrl($manager, $pathInfo); + $result = $rule->parseRequest($manager, $request); if ($route === false) { $this->assertFalse($result, "Test#$i-$j: $name"); } else { @@ -328,7 +330,7 @@ class UrlRuleTest extends \yiiunit\TestCase ); } - protected function getTestsForParseUrl() + protected function getTestsForParseRequest() { // structure of each test // message for the test