From bb1489dab1a340032c5aa2b4c961992fdfd2f2b8 Mon Sep 17 00:00:00 2001 From: Bizley Date: Tue, 26 Jan 2021 13:49:14 +0100 Subject: [PATCH] Fix #18493: Faster request parsing for REST UrlRule with prefix handling --- framework/CHANGELOG.md | 1 + framework/rest/UrlRule.php | 4 ++++ tests/framework/rest/UrlRuleTest.php | 2 ++ 3 files changed, 7 insertions(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 04109da178..fcd7402d8b 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -14,6 +14,7 @@ Yii Framework 2 Change Log - Bug #18479: Fix invalid argument type for `preg_split` in `\yii\console\Controller` (gazooz) - Bug #18477: Fix detecting availability of Xdebug's stack trace in `yii\base\ErrorException` (bizley) - Bug #18480: Transactions are not committed using the dblib driver (bbrunekreeft) +- Enh #18493: Faster request parsing for REST UrlRule with prefix handling (bizley) - Enh #18487: Allow creating URLs for non-GET-verb rules (bizley) diff --git a/framework/rest/UrlRule.php b/framework/rest/UrlRule.php index 3cee579930..413ecf3db0 100644 --- a/framework/rest/UrlRule.php +++ b/framework/rest/UrlRule.php @@ -216,6 +216,10 @@ class UrlRule extends CompositeUrlRule public function parseRequest($manager, $request) { $pathInfo = $request->getPathInfo(); + if ($this->prefix !== '' && strpos($pathInfo . '/', $this->prefix . '/') !== 0) { + return false; + } + foreach ($this->rules as $urlName => $rules) { if (strpos($pathInfo, $urlName) !== false) { foreach ($rules as $rule) { diff --git a/tests/framework/rest/UrlRuleTest.php b/tests/framework/rest/UrlRuleTest.php index 91ef385226..e52eb798e9 100644 --- a/tests/framework/rest/UrlRuleTest.php +++ b/tests/framework/rest/UrlRuleTest.php @@ -86,6 +86,8 @@ class UrlRuleTest extends TestCase ['controller' => 'post', 'prefix' => 'admin'], [ ['admin/posts', 'post/index'], + ['different/posts', false], + ['posts', false], ], ], [