1 | <?php |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
169 | } |
||
170 |