|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Helper methods when dealing with URLs and Links. |
|
9
|
|
|
* |
|
10
|
|
|
* Extends the {{yii\helpers\BaseUrl}} class by some usefull functions like: |
|
11
|
|
|
* |
|
12
|
|
|
* + {{luya\helpers\Url::trailing()}} |
|
13
|
|
|
* + {{luya\helpers\Url::toInternal()}} |
|
14
|
|
|
* + {{luya\helpers\Url::toAjax()}} |
|
15
|
|
|
* + {{luya\helpers\Url::ensureHttp()}} |
|
16
|
|
|
* |
|
17
|
|
|
* An example of create an URL based on Route in the UrlManager: |
|
18
|
|
|
* |
|
19
|
|
|
* ```php |
|
20
|
|
|
* Url::toRoute(['/module/controller/action']); |
|
21
|
|
|
* ``` |
|
22
|
|
|
* |
|
23
|
|
|
* @author Basil Suter <[email protected]> |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Url extends \yii\helpers\BaseUrl |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Add a trailing slash to an url if there is no trailing slash at the end of the url. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $url The url which a trailing slash should be appended |
|
32
|
|
|
* @param string $slash If you want to trail a file on a windows system it gives you the ability to add forward slashes. |
|
33
|
|
|
* @return string The url with added trailing slash, if requred. |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function trailing($url, $slash = '/') |
|
36
|
|
|
{ |
|
37
|
|
|
return rtrim($url, $slash) . $slash; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* This helper method will not concern any context informations |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $routeParams Example array to route `['/module/controller/action']`. |
|
44
|
|
|
* @param boolean $scheme Whether to return the absolute url or not |
|
45
|
|
|
* @return string The created url. |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function toInternal(array $routeParams, $scheme = false) |
|
48
|
|
|
{ |
|
49
|
|
|
if ($scheme) { |
|
50
|
|
|
return Yii::$app->getUrlManager()->internalCreateAbsoluteUrl($routeParams); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return Yii::$app->getUrlManager()->internalCreateUrl($routeParams); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create a link to use when point to an ajax script. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $route The base routing path defined in yii. module/controller/action |
|
60
|
|
|
* @param array $params Optional array containing get parameters with key value pairing |
|
61
|
|
|
* @return string The ajax url link. |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function toAjax($route, array $params = []) |
|
64
|
|
|
{ |
|
65
|
|
|
$routeParams = ['/'.$route]; |
|
66
|
|
|
foreach ($params as $key => $value) { |
|
67
|
|
|
$routeParams[$key] = $value; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return static::toInternal($routeParams, true); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Apply the http protcol to an url to make sure valid clickable links. Commonly used when provide link where user could have added urls |
|
75
|
|
|
* in an administration area. For Example: |
|
76
|
|
|
* |
|
77
|
|
|
* ```php |
|
78
|
|
|
* Url::ensureHttp('luya.io'); // return http://luya.io |
|
79
|
|
|
* Url::ensureHttp('www.luya.io'); // return https://luya.io |
|
80
|
|
|
* Url::ensureHttp('luya.io', true); // return https://luya.io |
|
81
|
|
|
* ``` |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $url The url where the http protcol should be applied to if missing |
|
84
|
|
|
* @param boolean $https Whether the ensured url should be returned as https or not. |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function ensureHttp($url, $https = false) |
|
88
|
|
|
{ |
|
89
|
|
|
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { |
|
90
|
|
|
$url = ($https ? "https://" : "http://") . $url; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $url; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Removes schema, protocol and www. subdomain from host. |
|
98
|
|
|
* |
|
99
|
|
|
* For example `https://luya.io/path` would return `luya.io/path`. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $url The url to extract |
|
102
|
|
|
* @return string returns the url without protocol or www. subdomain |
|
103
|
|
|
* @since 1.0.20 |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function cleanHost($url) |
|
106
|
|
|
{ |
|
107
|
|
|
return str_replace(['www.', 'http://', 'https://'], '', Url::ensureHttp($url)); |
|
108
|
|
|
} |
|
109
|
|
|
/** |
|
110
|
|
|
* Return only the domain of a path. |
|
111
|
|
|
* |
|
112
|
|
|
* For example `https://luya.io/path` would return `luya.io` without path informations. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $url The url to extract |
|
115
|
|
|
* @return string Returns only the domain from the url. |
|
116
|
|
|
* @since 1.0.20 |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function domain($url) |
|
119
|
|
|
{ |
|
120
|
|
|
return self::cleanHost(parse_url(Url::ensureHttp($url), PHP_URL_HOST)); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Append a query to the current url. |
|
125
|
|
|
* |
|
126
|
|
|
* See {{luya\helpers\Url::appendToUrl()}} |
|
127
|
|
|
* |
|
128
|
|
|
* @param string|array $append A string with url fragments or an array which will be processed by http_build_query. |
|
129
|
|
|
* @param boolean $scheme Add full path schema to the url, by default false. Otherwise absolute paths are used (including domain). |
|
130
|
|
|
* @return string |
|
131
|
|
|
* @since 1.0.25 |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function appendQuery($append, $scheme = false) |
|
134
|
|
|
{ |
|
135
|
|
|
$url = $scheme ? Yii::$app->request->absoluteUrl : Yii::$app->request->url; |
|
136
|
|
|
|
|
137
|
|
|
return self::appendQueryToUrl($url, $append); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Append an url part to an url |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $url The url where the data should be appended. |
|
144
|
|
|
* @param string|array $append The query param to append, if an array is given http_build_query() will taken to build the query string. |
|
145
|
|
|
* @return string Returns the url with appended query string |
|
146
|
|
|
* @since 1.0.25 |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function appendQueryToUrl($url, $append) |
|
149
|
|
|
{ |
|
150
|
|
|
if (is_array($append)) { |
|
151
|
|
|
$append = http_build_query($append); |
|
152
|
|
|
} |
|
153
|
|
|
// remove starting & and ? chars |
|
154
|
|
|
$append = ltrim($append, '&?'); |
|
155
|
|
|
// use &: Do we have already a ? in the url |
|
156
|
|
|
if (StringHelper::contains('?', $url)) { |
|
157
|
|
|
// seperator already existing |
|
158
|
|
|
if (StringHelper::endsWith($url, '&') || StringHelper::endsWith($url, '?')) { |
|
159
|
|
|
return $url . $append; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// add seperator |
|
163
|
|
|
return $url . '&' . $append; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// use ? |
|
167
|
|
|
return $url . '?' . $append; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|