Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit da907cb

Browse files
committed
Merge branch 'issue3'
2 parents 1280065 + 13df104 commit da907cb

File tree

7 files changed

+118
-59
lines changed

7 files changed

+118
-59
lines changed

js/build/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/UrlManager.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export default class UrlManager {
2525
* @type {string}
2626
*/
2727
private suffix: string = '';
28+
/**
29+
* Prefix for url, depends on enablePrettyUrl and showScriptName settings
30+
* Can contain something looks like '/index.php'
31+
* Or baseUrl, if application document root is in subdirectories
32+
* @type {string}
33+
*/
34+
private prefix: string = '';
2835

2936
public configure(config: IUrlManagerConfig) {
3037
if (config.enablePrettyUrl !== undefined) {
@@ -36,6 +43,9 @@ export default class UrlManager {
3643
if (config.suffix !== undefined) {
3744
this.suffix = config.suffix;
3845
}
46+
if (config.prefix !== undefined) {
47+
this.prefix = config.prefix;
48+
}
3949
this.buildRules(config.rules);
4050
}
4151

@@ -79,8 +89,8 @@ export default class UrlManager {
7989
result = UrlManager.createQueryUrl(route, urlParams);
8090
}
8191

82-
if (this.showScriptName || !this.enablePrettyUrl) {
83-
result = '/index.php' + result;
92+
if (this.prefix) {
93+
result = this.prefix + result;
8494
}
8595

8696
return result;

js/src/interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export interface IUrlManagerConfig {
1212
enablePrettyUrl?: boolean;
1313
showScriptName?: boolean;
1414
rules: IRule[],
15-
suffix?: string
15+
suffix?: string,
16+
prefix?: string,
1617
}

js/tests/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ test('it should create url if showScriptName is true', t => {
101101
urlManager.configure({
102102
enablePrettyUrl: true,
103103
showScriptName: true,
104+
prefix : '/index.php',
104105
rules: [
105106
{
106107
name: '/foo/<id:(\\d+)>/bar/<type:(first|second)>',
@@ -135,6 +136,7 @@ test('it should create url if suffix passed', t => {
135136
enablePrettyUrl: true,
136137
showScriptName: false,
137138
suffix : '.html',
139+
prefix : '',
138140
rules: [
139141
{
140142
name: '/',

src/JsUrlManager.php

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ class JsUrlManager extends Object implements BootstrapInterface
3232
*/
3333
public $configureThroughVariable = false;
3434

35+
/**
36+
* @var UrlManager
37+
*/
38+
private $urlManager;
39+
3540
/**
3641
* @inheritdoc
3742
*/
3843
public function bootstrap($app)
3944
{
40-
$configuration = $this->defineConfiguration($app->urlManager);
45+
$this->setUrlManager($app->urlManager);
46+
$configuration = $this->defineConfiguration();
4147
if ($this->configureThroughVariable) {
4248
$this->configureFrontendUrlManagerThroughVariable($configuration);
4349
} else {
@@ -57,13 +63,26 @@ public static function registerAssets()
5763

5864
/**
5965
* Define configuration based on yii\web\UrlManager's configuration
60-
* @param UrlManager $urlManager
6166
* @return array
6267
*/
63-
public function defineConfiguration(UrlManager $urlManager)
68+
public function defineConfiguration()
69+
{
70+
return [
71+
'enablePrettyUrl' => $this->urlManager->enablePrettyUrl,
72+
'showScriptName' => $this->urlManager->showScriptName,
73+
'suffix' => $this->urlManager->suffix,
74+
'rules' => $this->getRules(),
75+
'prefix' => $this->getPrefix(),
76+
];
77+
}
78+
79+
/**
80+
* Get rules
81+
*/
82+
public function getRules()
6483
{
6584
$rules = [];
66-
foreach ($urlManager->rules as $name => $rule) {
85+
foreach ($this->urlManager->rules as $name => $rule) {
6786
if ($rule instanceof UrlRule) {
6887
$rules[] = [
6988
'name' => $rule->name,
@@ -76,15 +95,35 @@ public function defineConfiguration(UrlManager $urlManager)
7695
'route' => $rule
7796
];
7897
}
79-
8098
}
8199

82-
return [
83-
'enablePrettyUrl' => $urlManager->enablePrettyUrl,
84-
'showScriptName' => $urlManager->showScriptName,
85-
'suffix' => $urlManager->suffix,
86-
'rules' => $rules
87-
];
100+
return $rules;
101+
}
102+
103+
/**
104+
* Get url's prefix
105+
* @return string
106+
*/
107+
public function getPrefix()
108+
{
109+
return $this->urlManager->showScriptName || !$this->urlManager->enablePrettyUrl ?
110+
$this->urlManager->getScriptUrl() : $this->urlManager->getBaseUrl();
111+
}
112+
113+
/**
114+
* @return UrlManager
115+
*/
116+
public function getUrlManager()
117+
{
118+
return $this->urlManager;
119+
}
120+
121+
/**
122+
* @param UrlManager $urlManager
123+
*/
124+
public function setUrlManager(UrlManager $urlManager)
125+
{
126+
$this->urlManager = $urlManager;
88127
}
89128

90129
/**

tests/JsUrlManagerTest.php

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,66 @@
33

44
class JsUrlManagerTest extends \PHPUnit\Framework\TestCase
55
{
6-
public function testDefineConfiguration()
6+
/**
7+
* @return \yii\web\UrlManager
8+
*/
9+
protected function mockUrlManager()
710
{
8-
/** @var \dmirogin\js\urlmanager\JsUrlManager $jsUrlManager */
9-
$jsUrlManager = Yii::$app->jsUrlManager;
10-
$this->assertEquals([
11+
/** @var \yii\web\UrlManager $urlManager */
12+
$urlManager = Yii::createObject([
13+
'class' => \yii\web\UrlManager::class,
1114
'enablePrettyUrl' => true,
1215
'showScriptName' => false,
1316
'suffix' => '.html',
1417
'rules' => [
18+
'/' => '/site/index',
19+
'/add/<id:\d+>' => '/site/add',
1520
[
16-
'name' => '/',
17-
'route' => 'site/index',
18-
'suffix' => null
19-
],
20-
[
21-
'name' => '/add/<id:\d+>',
22-
'route' => 'site/add',
23-
'suffix' => null
21+
'pattern' => '/foo/<id:(\\d+)>/bar/<type:(first|second)>/',
22+
'route' => '/foo/bar',
23+
'suffix' => '/'
2424
],
2525
[
26-
'name' => '/foo/<id:(\\d+)>/bar/<type:(first|second)>/',
27-
'route' => 'foo/bar',
28-
'suffix' => '/'
26+
'class' => \yii\web\GroupUrlRule::class,
27+
'prefix' => 'admin',
28+
'rules' => [
29+
'login' => 'user/login',
30+
'logout' => 'user/logout',
31+
'dashboard' => 'default/dashboard',
32+
],
2933
]
34+
],
35+
]);
36+
37+
return $urlManager;
38+
}
39+
40+
public function testGetRules()
41+
{
42+
$urlManager = $this->mockUrlManager();
43+
44+
/** @var \dmirogin\js\urlmanager\JsUrlManager $jsUrlManager */
45+
$jsUrlManager = Yii::createObject(\dmirogin\js\urlmanager\JsUrlManager::class);
46+
$jsUrlManager->setUrlManager($urlManager);
47+
48+
$this->assertEquals([
49+
[
50+
'name' => '/',
51+
'route' => 'site/index',
52+
'suffix' => null
53+
],
54+
[
55+
'name' => '/add/<id:\d+>',
56+
'route' => 'site/add',
57+
'suffix' => null
58+
],
59+
[
60+
'name' => '/foo/<id:(\\d+)>/bar/<type:(first|second)>/',
61+
'route' => 'foo/bar',
62+
'suffix' => '/'
3063
]
31-
], $jsUrlManager->defineConfiguration(Yii::$app->urlManager));
64+
], $jsUrlManager->getRules());
3265
}
66+
67+
3368
}

tests/bootstrap.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,4 @@
88
new \yii\web\Application([
99
'id' => 'unit',
1010
'basePath' => __DIR__,
11-
'components' => [
12-
'jsUrlManager' => [
13-
'class' => \dmirogin\js\urlmanager\JsUrlManager::class,
14-
],
15-
'urlManager' => [
16-
'enablePrettyUrl' => true,
17-
'showScriptName' => false,
18-
'suffix' => '.html',
19-
'rules' => [
20-
'/' => '/site/index',
21-
'/add/<id:\d+>' => '/site/add',
22-
[
23-
'pattern' => '/foo/<id:(\\d+)>/bar/<type:(first|second)>/',
24-
'route' => '/foo/bar',
25-
'suffix' => '/'
26-
],
27-
[
28-
'class' => \yii\web\GroupUrlRule::class,
29-
'prefix' => 'admin',
30-
'rules' => [
31-
'login' => 'user/login',
32-
'logout' => 'user/logout',
33-
'dashboard' => 'default/dashboard',
34-
],
35-
]
36-
],
37-
]
38-
]
3911
]);

0 commit comments

Comments
 (0)