Skip to content

Commit

Permalink
remove hard coded request param string identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Stebe committed Mar 14, 2017
1 parent c221af2 commit 21431ed
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
13 changes: 7 additions & 6 deletions Bootstrap.php
Expand Up @@ -10,6 +10,7 @@
namespace dmstr\modules\pages;

use dmstr\modules\pages\components\PageUrlRule;
use dmstr\modules\pages\models\Tree;
use yii\base\Application;
use yii\base\BootstrapInterface;

Expand Down Expand Up @@ -51,14 +52,14 @@ public function bootstrap($app)
[
// pages default page route
['class' => PageUrlRule::className()],
'p/<pagePath:[a-zA-Z0-9_\-\./\+]*>/<pageSlug:[a-zA-Z0-9_\-\.]*>-<pageId:[0-9]*>.html' => 'pages/default/page',
'p/<pageSlug:[a-zA-Z0-9_\-\.]*>-<pageId:[0-9]*>.html' => 'pages/default/page',
'p/<'.Tree::REQUEST_PARAM_PATH.':[a-zA-Z0-9_\-\./\+]*>/<'.Tree::REQUEST_PARAM_SLUG.':[a-zA-Z0-9_\-\.]*>-<'.Tree::REQUEST_PARAM_ID.':[0-9]*>.html' => 'pages/default/page',
'p/<'.Tree::REQUEST_PARAM_SLUG.':[a-zA-Z0-9_\-\.]*>-<'.Tree::REQUEST_PARAM_ID.':[0-9]*>.html' => 'pages/default/page',

// Backward compatibility
'page/<pagePath:[a-zA-Z0-9_\-\./\+]*>/<pageSlug:[a-zA-Z0-9_\-\.]*>-<pageId:[0-9]*>.html' => 'pages/default/page',
'page/<pageSlug:[a-zA-Z0-9_\-\.]*>-<pageId:[0-9]*>.html' => 'pages/default/page',
'<pagePath:[a-zA-Z0-9_\-\./\+]*>/<pageSlug:[a-zA-Z0-9_\-\.]*>-<pageId:[0-9]*>' => 'pages/default/page',
'<pageSlug:[a-zA-Z0-9_\-\.]*>-<pageId:[0-9]*>' => 'pages/default/page',
'page/<'.Tree::REQUEST_PARAM_PATH.':[a-zA-Z0-9_\-\./\+]*>/<'.Tree::REQUEST_PARAM_SLUG.':[a-zA-Z0-9_\-\.]*>-<'.Tree::REQUEST_PARAM_ID.':[0-9]*>.html' => 'pages/default/page',
'page/<'.Tree::REQUEST_PARAM_SLUG.':[a-zA-Z0-9_\-\.]*>-<'.Tree::REQUEST_PARAM_ID.':[0-9]*>.html' => 'pages/default/page',
'<'.Tree::REQUEST_PARAM_PATH.':[a-zA-Z0-9_\-\./\+]*>/<'.Tree::REQUEST_PARAM_SLUG.':[a-zA-Z0-9_\-\.]*>-<'.Tree::REQUEST_PARAM_ID.':[0-9]*>' => 'pages/default/page',
'<'.Tree::REQUEST_PARAM_SLUG.':[a-zA-Z0-9_\-\.]*>-<'.Tree::REQUEST_PARAM_ID.':[0-9]*>' => 'pages/default/page',
],
true
);
Expand Down
18 changes: 9 additions & 9 deletions components/PageUrlRule.php
Expand Up @@ -35,21 +35,21 @@ public function createUrl($manager, $route, $params)
* Build page url
*/
$pageId = '';
if (isset($params['pageId'])) {
$pageId = '-' . $params['pageId'];
unset($params['pageId']);
if (isset($params[Tree::REQUEST_PARAM_ID])) {
$pageId = '-' . $params[Tree::REQUEST_PARAM_ID];
unset($params[Tree::REQUEST_PARAM_ID]);
}

$pageSlug = '';
if (isset($params['pageSlug'])) {
$pageSlug = $params['pageSlug'];
unset($params['pageSlug']);
if (isset($params[Tree::REQUEST_PARAM_SLUG])) {
$pageSlug = $params[Tree::REQUEST_PARAM_SLUG];
unset($params[Tree::REQUEST_PARAM_SLUG]);
}

$pagePath = '';
if (isset($params['pagePath'])) {
$pagePath = $params['pagePath'] . '/';
unset($params['pagePath']);
if (isset($params[Tree::REQUEST_PARAM_PATH])) {
$pagePath = $params[Tree::REQUEST_PARAM_PATH] . '/';
unset($params[Tree::REQUEST_PARAM_PATH]);
}

$pageUrl = $pagePath . $pageSlug . $pageId;
Expand Down
15 changes: 15 additions & 0 deletions models/BaseTree.php
Expand Up @@ -109,6 +109,21 @@ class BaseTree extends \kartik\tree\models\Tree
*/
const DEFAULT_PAGE_ROUTE = '/pages/default/page';

/**
* The request param for a page identifier
*/
const REQUEST_PARAM_ID = 'pageId';

/**
* The request param for a page slug
*/
const REQUEST_PARAM_SLUG = 'pageSlug';

/**
* The request param for a page path
*/
const REQUEST_PARAM_PATH = 'pagePath';

/**
* Column attribute 'id'
*/
Expand Down
10 changes: 5 additions & 5 deletions models/Tree.php
Expand Up @@ -167,9 +167,9 @@ public function createRoute($additionalParams = [])

$route = [
$this->route,
'pageId' => $pageId,
'pageSlug' => $slug,
'pagePath' => $slugFolder
self::REQUEST_PARAM_ID => $pageId,
self::REQUEST_PARAM_SLUG => $slug,
self::REQUEST_PARAM_PATH => $slugFolder
];

if (Json::decode($this->request_params)) {
Expand Down Expand Up @@ -204,8 +204,8 @@ public function createUrl($additionalParams = [])
public static function getMenuItems($domainId, $checkUserPermissions = false)
{
// Get root node by domain id
$rootCondition['domain_id'] = $domainId;
$rootCondition['access_domain'] = [self::GLOBAL_ACCESS_DOMAIN,mb_strtolower(\Yii::$app->language)];
$rootCondition[self::ATTR_DOMAIN_ID] = $domainId;
$rootCondition[self::ATTR_ACCESS_DOMAIN] = [self::GLOBAL_ACCESS_DOMAIN,mb_strtolower(\Yii::$app->language)];
if (!Yii::$app->user->can('pages')) {
$rootCondition[self::ATTR_DISABLED] = self::NOT_DISABLED;
}
Expand Down
11 changes: 6 additions & 5 deletions tests/codeception/unit/UrlTest.php
Expand Up @@ -5,6 +5,7 @@

use Codeception\Util\Debug;
use dmstr\modules\pages\components\PageUrlRule;
use dmstr\modules\pages\models\Tree;

class UrlTestCase extends \Codeception\Test\Unit
{
Expand All @@ -26,7 +27,7 @@ public function testUrlRuleParams()
* Check url with params
* - pageId
*/
$params = ['pageId' => 1];
$params = [Tree::REQUEST_PARAM_ID => 1];

$createdUrl = $rule->createUrl($urlManager, $route, $params);
$expectedUrl = '-1';
Expand All @@ -38,7 +39,7 @@ public function testUrlRuleParams()
* - pageId
* - pageSlug
*/
$params = ['pageId' => 1, 'pageSlug' => 'slug'];
$params = [Tree::REQUEST_PARAM_ID => 1, Tree::REQUEST_PARAM_SLUG => 'slug'];

$createdUrl = $rule->createUrl($urlManager, $route, $params);
$expectedUrl = 'slug-1';
Expand All @@ -51,7 +52,7 @@ public function testUrlRuleParams()
* - pageSlug
* - pagePath
*/
$params = ['pageId' => 1, 'pageSlug' => 'slug', 'pagePath' => 'subpage/next-subpage/next-subpage'];
$params = [Tree::REQUEST_PARAM_ID => 1, Tree::REQUEST_PARAM_SLUG => 'slug', Tree::REQUEST_PARAM_PATH => 'subpage/next-subpage/next-subpage'];

$createdUrl = $rule->createUrl($urlManager, $route, $params);
$expectedUrl = 'subpage/next-subpage/next-subpage/slug-1';
Expand Down Expand Up @@ -95,15 +96,15 @@ public function testUrlRuleParams()
* - param1 => value1
*/
$route = 'static-route';
$params = ['pageId' => 5, 'param1' => 'value1'];
$params = [Tree::REQUEST_PARAM_ID => 5, 'param1' => 'value1'];
$createdUrl = $rule->createUrl($urlManager, $route, $params);

/**
* if not pages/default/page route, the PageUrlRule will not match
* and the application url manager will be used
*/
if ($createdUrl === false) {
$params = [0 => '/static-route', 'pageId' => 5, 'param1' => 'value1'];
$params = [0 => '/static-route', Tree::REQUEST_PARAM_ID => 5, 'param1' => 'value1'];

$createdUrl = $urlManager->createUrl($params);
}
Expand Down

0 comments on commit 21431ed

Please sign in to comment.