Commit a095d383 by Qiang Xue

refactored url management.

parent de19d33d
...@@ -32,7 +32,7 @@ class Application extends \yii\base\Application ...@@ -32,7 +32,7 @@ class Application extends \yii\base\Application
*/ */
public function processRequest() public function processRequest()
{ {
$route = $this->getUrlManager()->parseUrl($this->getRequest()); $route = $this->getUrlManager()->parseRequest($this->getRequest());
return $this->runAction($route, $_GET); return $this->runAction($route, $_GET);
} }
......
...@@ -119,7 +119,7 @@ class UrlManager extends Component ...@@ -119,7 +119,7 @@ class UrlManager extends Component
$pathInfo = $request->pathInfo; $pathInfo = $request->pathInfo;
/** @var $rule UrlRule */ /** @var $rule UrlRule */
foreach ($this->rules as $rule) { foreach ($this->rules as $rule) {
if (($result = $rule->parseUrl($this, $pathInfo)) !== false) { if (($result = $rule->parseRequest($this, $request)) !== false) {
return $result; return $result;
} }
} }
......
...@@ -57,10 +57,10 @@ class UrlRule extends Object ...@@ -57,10 +57,10 @@ class UrlRule extends Object
*/ */
public $verb; 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. * parsing only, or creation only.
* If not set, it means the rule is both URL parsing and creation. * 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 URL parsing only. * If it is [[PARSING_ONLY]], the rule is for request parsing only.
* If it is [[CREATION_ONLY]], the rule is for URL creation only. * If it is [[CREATION_ONLY]], the rule is for URL creation only.
*/ */
public $mode; public $mode;
...@@ -152,22 +152,23 @@ class UrlRule extends Object ...@@ -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 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. * @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. * 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) { if ($this->mode === self::CREATION_ONLY) {
return false; 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; return false;
} }
$pathInfo = $request->pathInfo;
$suffix = (string)($this->suffix === null ? $manager->suffix : $this->suffix); $suffix = (string)($this->suffix === null ? $manager->suffix : $this->suffix);
if ($suffix !== '' && $pathInfo !== '') { if ($suffix !== '' && $pathInfo !== '') {
$n = strlen($suffix); $n = strlen($suffix);
......
...@@ -4,6 +4,7 @@ namespace yiiunit\framework\web; ...@@ -4,6 +4,7 @@ namespace yiiunit\framework\web;
use yii\web\UrlManager; use yii\web\UrlManager;
use yii\web\UrlRule; use yii\web\UrlRule;
use yii\web\Request;
class UrlRuleTest extends \yiiunit\TestCase class UrlRuleTest extends \yiiunit\TestCase
{ {
...@@ -22,18 +23,19 @@ class UrlRuleTest extends \yiiunit\TestCase ...@@ -22,18 +23,19 @@ class UrlRuleTest extends \yiiunit\TestCase
} }
} }
public function testParseUrl() public function testParseRequest()
{ {
$manager = new UrlManager; $manager = new UrlManager;
$suites = $this->getTestsForParseUrl(); $request = new Request;
$suites = $this->getTestsForParseRequest();
foreach ($suites as $i => $suite) { foreach ($suites as $i => $suite) {
list ($name, $config, $tests) = $suite; list ($name, $config, $tests) = $suite;
$rule = new UrlRule($config); $rule = new UrlRule($config);
foreach ($tests as $j => $test) { foreach ($tests as $j => $test) {
$pathInfo = $test[0]; $request->pathInfo = $test[0];
$route = $test[1]; $route = $test[1];
$params = isset($test[2]) ? $test[2] : array(); $params = isset($test[2]) ? $test[2] : array();
$result = $rule->parseUrl($manager, $pathInfo); $result = $rule->parseRequest($manager, $request);
if ($route === false) { if ($route === false) {
$this->assertFalse($result, "Test#$i-$j: $name"); $this->assertFalse($result, "Test#$i-$j: $name");
} else { } else {
...@@ -328,7 +330,7 @@ class UrlRuleTest extends \yiiunit\TestCase ...@@ -328,7 +330,7 @@ class UrlRuleTest extends \yiiunit\TestCase
); );
} }
protected function getTestsForParseUrl() protected function getTestsForParseRequest()
{ {
// structure of each test // structure of each test
// message for the test // message for the test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment