-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManagerUrl.php
103 lines (91 loc) · 2.98 KB
/
ManagerUrl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace panix\engine;
use Yii;
use yii\web\UrlManager;
class ManagerUrl extends UrlManager
{
public function init()
{
// $this->modulesRoutes();
parent::init();
}
public function createUrl($params, $respectLang = true)
{
$result = parent::createUrl($params);
if ($respectLang === true) {
$langPrefix = Yii::$app->languageManager->getUrlPrefix();
if ($langPrefix) {
$result = '/' . $langPrefix . $result;
}
}
return $result;
}
/**
* BootstrapModule Class
*/
protected function modulesRoutes()
{
$cacheKey = 'url_manager';
$rules = Yii::$app->cache->get($cacheKey);
if (YII_DEBUG || !$rules) {
$modules = Yii::$app->getModules();
$rules = array();
foreach ($modules as $mod => $params) {
$run = Yii::$app->getModule($mod, true);
if (isset($run->routes)) {
$rules = array_merge($run->routes, $rules);
}
}
Yii::$app->cache->set($cacheKey, $rules, 3600 * 24);
}
$this->rules = array_merge($rules, $this->rules);
}
/**
* Add param to current url. Url is based on $data and $_GET arrays
*
* @param $route
* @param $data array of the data to add to the url.
* @param $selectMany
* @return string
*/
public function addUrlParam($route, $data, $selectMany = false)
{
foreach ($data as $key => $val) {
if (isset($_GET[$key]) && $key !== 'url' && $selectMany === true) {
$tempData = explode(',', $_GET[$key]);
$data[$key] = implode(',', array_unique(array_merge((array)$data[$key], $tempData)));
}
}
return $this->createUrl(array_merge([$route], array_merge($_GET, $data)));
}
/**
* Delete param/value from current
*
* @param string $route
* @param string $key to remove from query
* @param null $value If not value - delete whole key
* @return string new url
*/
public function removeUrlParam($route, $key, $value = null)
{
$get = Yii::$app->request->get();
if (isset($get[$key])) {
if ($value === null)
unset($get[$key]);
else {
$get[$key] = explode(',', $get[$key]);
$pos = array_search($value, $get[$key]);
// Delete value
if (isset($get[$key][$pos]))
unset($get[$key][$pos]);
// Save changes
if (!empty($get[$key])) {
$get[$key] = implode(',', $get[$key]);
} // Delete key if empty
else
unset($get[$key]);
}
}
return $this->createUrl(array_merge([$route], $get));
}
}