diff --git a/framework/yii/web/UrlManager.php b/framework/yii/web/UrlManager.php index dde10ee..5a3c391 100644 --- a/framework/yii/web/UrlManager.php +++ b/framework/yii/web/UrlManager.php @@ -27,6 +27,12 @@ class UrlManager extends Component */ public $enablePrettyUrl = false; /** + * @var boolean whether to enable strict parsing. If strict parsing is enabled, the incoming + * requested URL must match at least one of the [[rules]] in order to be treated as a valid request. + * This property is used only when [[enablePrettyUrl]] is true. + */ + public $enableStrictParsing = false; + /** * @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is true. * This property is used only if [[enablePrettyUrl]] is true. Each element in the array * is the configuration array for creating a single URL rule. The configuration will @@ -139,6 +145,10 @@ class UrlManager extends Component } } + if ($this->enableStrictParsing) { + return false; + } + $suffix = (string)$this->suffix; if ($suffix !== '' && $suffix !== '/' && $pathInfo !== '') { $n = strlen($this->suffix); diff --git a/tests/unit/framework/web/UrlManagerTest.php b/tests/unit/framework/web/UrlManagerTest.php index b6246c6..553b163 100644 --- a/tests/unit/framework/web/UrlManagerTest.php +++ b/tests/unit/framework/web/UrlManagerTest.php @@ -224,5 +224,27 @@ class UrlManagerTest extends \yiiunit\TestCase $request->pathInfo = 'site/index'; $result = $manager->parseRequest($request); $this->assertFalse($result); + + // strict parsing + $manager = new UrlManager(array( + 'enablePrettyUrl' => true, + 'enableStrictParsing' => true, + 'suffix' => '.html', + 'cache' => null, + 'rules' => array( + array( + 'pattern' => 'post//', + 'route' => 'post/view', + ), + ), + )); + // matching pathinfo + $request->pathInfo = 'post/123/this+is+sample.html'; + $result = $manager->parseRequest($request); + $this->assertEquals(array('post/view', array('id' => '123', 'title' => 'this+is+sample')), $result); + // unmatching pathinfo + $request->pathInfo = 'site/index.html'; + $result = $manager->parseRequest($request); + $this->assertFalse($result); } }