From 54e3e916522b0520e05952a636803a5103ef287a Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 14 Aug 2021 13:35:09 +0300 Subject: [PATCH] fix urlRule classes --- Module.php | 18 +++++++----- README_RU.md | 7 ++--- components/BaseTest2UrlRule.php | 40 ++++++++++++++++++++------- components/CategoryUrlRuleNew.php | 5 ++-- components/FilterController.php | 1 + controllers/CatalogController.php | 5 +++- widgets/filtersnew2/views/default.php | 3 +- 7 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Module.php b/Module.php index 642af68..319cc94 100644 --- a/Module.php +++ b/Module.php @@ -10,6 +10,7 @@ use panix\engine\WebModule; use yii\base\BootstrapInterface; use app\web\themes\dashboard\sidebar\BackendNav; +use yii\web\UrlNormalizer; class Module extends WebModule implements BootstrapInterface { @@ -170,21 +171,24 @@ public function bootstrap($app) } + $rules[] = [ + 'class' => 'panix\mod\shop\components\BaseTest2UrlRule', + 'route' => 'shop/catalog/sales', + 'index' => 'sales', + 'pattern' => 'sales', + ]; + + $rules[] = [ 'class' => 'panix\mod\shop\components\BaseTest2UrlRule', - 'route' => '/shop/catalog/new', + 'route' => 'shop/catalog/new', 'index' => 'new', 'pattern' => 'new' ]; ///////////////////////////////////////////// - $rules[] = [ - 'class' => 'panix\mod\shop\components\BaseTest2UrlRule', - 'route' => '/shop/catalog/sales', - 'index' => 'sales', - 'pattern' => 'sales' - ]; + /*$rules[] = [ 'class' => 'app\engine\BaseUrlRule', 'route' => 'shop/catalog/best', diff --git a/README_RU.md b/README_RU.md index 2972ef2..3273ad1 100644 --- a/README_RU.md +++ b/README_RU.md @@ -56,10 +56,9 @@ php yii migrate --migrationPath=vendor/panix/mod-shop/migrations - /new | catalog | sales | new | -|----------|:-------------:|------:| -| col 1 is | `$categories` | $1600 | -| col 2 is | centered | $12 | -| col 3 is | right-aligned | $1 | +|-----------|:-------------:|------:| +| --- |`$categories`| --- | +| --- | --- | --- | ### Автор и лицензия diff --git a/components/BaseTest2UrlRule.php b/components/BaseTest2UrlRule.php index ca15dfb..2d39d63 100644 --- a/components/BaseTest2UrlRule.php +++ b/components/BaseTest2UrlRule.php @@ -24,7 +24,7 @@ public function createUrl($manager, $route, $params) { if ($route === $this->route) { if (isset($params['slug'])) { - $url = trim($params['slug'], '/'); + $url = '/'.trim($params['slug'], '/'); unset($params['slug']); } else { $url = ''; @@ -38,6 +38,7 @@ public function createUrl($manager, $route, $params) } $url .= '/' . implode('/', $parts); } + return $this->index . $url . $this->suffix; } return false; @@ -47,17 +48,36 @@ public function createUrl($manager, $route, $params) public function parseRequest($manager, $request) { - $params = []; - $pathInfo = $request->getPathInfo(); - - $basePathInfo = $pathInfo; - if (empty($pathInfo)) + //original begin + if ($this->mode === self::CREATION_ONLY) { return false; + } - if ($this->suffix) - $pathInfo = strtr($pathInfo, [$this->suffix => '']); + if (!empty($this->verb) && !in_array($request->getMethod(), $this->verb, true)) { + return false; + } + $suffix = (string) ($this->suffix === null ? $manager->suffix : $this->suffix); + $pathInfo = $request->getPathInfo(); + $normalized = false; + if ($this->hasNormalizer($manager)) { + $pathInfo = $this->getNormalizer($manager)->normalizePathInfo($pathInfo, $suffix, $normalized); + } + if ($suffix !== '' && $pathInfo !== '') { + $n = strlen($suffix); + if (substr_compare($pathInfo, $suffix, -$n, $n) === 0) { + $pathInfo = substr($pathInfo, 0, -$n); + if ($pathInfo === '') { + // suffix alone is not allowed + return false; + } + } else { + return false; + } + } + //original end + $params=[]; $pathInfoParse = str_replace($this->index . '/', '', $pathInfo); $parts = explode('/', $pathInfoParse); if ($this->index == mb_substr($pathInfo, 0,strlen($this->index))) { @@ -78,8 +98,8 @@ public function parseRequest($manager, $request) return false; }*/ - // CMS::dump([$this->route, $params]); - // die; + // CMS::dump([$this->route, $params]); + //die; return [$this->route, $params]; } return false; diff --git a/components/CategoryUrlRuleNew.php b/components/CategoryUrlRuleNew.php index 8bcbc46..23bdca3 100644 --- a/components/CategoryUrlRuleNew.php +++ b/components/CategoryUrlRuleNew.php @@ -94,11 +94,12 @@ public function createUrl($manager, $route, $params) if ($route === $this->route) { if (isset($params['slug'])) { - $url = trim($params['slug'], '/'); + $url = '/'.trim($params['slug'], '/'); unset($params['slug']); } else { $url = ''; } + //echo $url;die; $parts = []; if (!empty($params)) { foreach ($params as $key => $val) { @@ -110,7 +111,7 @@ public function createUrl($manager, $route, $params) $url .= '/' . implode('/', $parts); } - return $this->index.'/' . $url . $this->suffix; + return $this->index . $url . $this->suffix; // return $url . $this->suffix; } return parent::createUrl($manager, $route, $params); diff --git a/components/FilterController.php b/components/FilterController.php index 5949d29..633bf1b 100644 --- a/components/FilterController.php +++ b/components/FilterController.php @@ -62,6 +62,7 @@ class FilterController extends WebController public $_maxPrice, $_minPrice; public $currentUrl; + public $refreshUrl; public $itemView = '_view_grid'; public $per_page; public $filter; diff --git a/controllers/CatalogController.php b/controllers/CatalogController.php index 0154104..b4240bc 100644 --- a/controllers/CatalogController.php +++ b/controllers/CatalogController.php @@ -74,6 +74,7 @@ public function actionView() $this->pageName = $this->dataModel->name; $this->view->setModel($this->dataModel); //$this->view->title = $this->pageName; + $this->refreshUrl= $this->dataModel->getUrl(); $this->view->registerJs("var current_url = '" . Url::to($this->dataModel->getUrl()) . "';", yii\web\View::POS_HEAD, 'current_url'); @@ -382,7 +383,7 @@ public function actionSales() $this->query->applyCategories(array_unique($categories), 'orWhere'); } - $this->currentUrl = Url::to(['sales']); + $this->currentUrl = Url::to(['/shop/catalog/sales']); $this->pageName = Yii::t('shop/default', 'DISCOUNT'); $categoriesIds=[]; @@ -396,6 +397,7 @@ public function actionSales() $category = $this->findModel(Yii::$app->request->getQueryParam('slug')); $this->query->applyCategories($category); $this->currentUrl = Url::to($category->getUrl('sales')); + $this->view->params['breadcrumbs'][] = [ 'url'=>['/shop/catalog/sales'], 'label'=>$this->pageName @@ -404,6 +406,7 @@ public function actionSales() $this->pageName = $category->name; } + $this->refreshUrl= $this->currentUrl; $this->view->params['breadcrumbs'][] = $this->pageName; diff --git a/widgets/filtersnew2/views/default.php b/widgets/filtersnew2/views/default.php index a15e53e..8d01490 100644 --- a/widgets/filtersnew2/views/default.php +++ b/widgets/filtersnew2/views/default.php @@ -15,7 +15,8 @@ 'ajax_filter_current']); if (!empty($active)) { - $url = ($this->model) ? $this->model->getUrl() : ['/' . Yii::$app->requestedRoute]; + // echo Yii::$app->controller->refreshUrl; + $url = Yii::$app->controller->refreshUrl; echo $this->render(Yii::$app->getModule('shop')->filterViewCurrent, ['active' => $active, 'dataModel' => $this->context->model, 'url' => $url]); } echo Html::endTag('div');