-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlHelper.php
154 lines (133 loc) · 2.88 KB
/
UrlHelper.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use Tamedevelopers\Support\Str;
use Tamedevelopers\Support\Server;
class UrlHelper {
/**
* Get the URL
*
* @return string
*/
static public function url()
{
// create from .env APP_URL or Default path
$url = env('APP_URL') ?? self::full();
return Str::trim($url, '\/');
}
/**
* Get Server Path
*
* @return string|null
*/
static public function server()
{
return self::getServerPath();
}
/**
* Get Request Url
*
* @return string|null
*/
static public function request()
{
$request = $_SERVER['REQUEST_URI'] ?? null;
return str_replace(self::path(), '', $request);
}
/**
* Get Referral Url
*
* @return string|null
*/
static public function referral()
{
return $_SERVER['HTTP_REFERER'] ?? null;
}
/**
* Get URL HTTP
*
* @return string|null
*/
static public function http()
{
return isset($_SERVER['HTTPS']) && Str::lower($_SERVER['HTTPS']) !== 'off' ? 'https://' : 'http://';
}
/**
* Get URL Host
*
* @return string|null
*/
static public function host()
{
return isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
}
/**
* Get URL Fullpath
*
* @return string|null
*/
static public function full()
{
return self::http() . self::host() . self::path();
}
/**
* Get URL Root Path
*
* @param string|null $path
* @return string|null
*/
static public function path($path = null)
{
if (!empty($path)) {
$path = ltrim($path, '/');
$path = self::replace($path);
}
return self::localDomainPath() . "{$path}";
}
/**
* Is IP accessed via 127.0.0.1 port in browser
*
* @return bool
*/
static public function isIpAccessedVia127Port()
{
return Str::contains(
$_SERVER['REMOTE_ADDR'] ?? '',
self::host()
);
}
/**
* Local Domain Path
*
* @return array
*/
static private function localDomainPath()
{
$domainPath = str_replace(
$_SERVER['DOCUMENT_ROOT'],
'',
self::getServerPath()
);
return self::isIpAccessedVia127Port() ? '/' : $domainPath;
}
/**
* Get server path
*
* @param string|null $path
* @return string
*/
static private function replace($path = null)
{
return Server::pathReplacer($path);
}
/**
* Get server path
* @return string
*/
static private function getServerPath()
{
return Server::cleanServerPath(
Server::createAbsolutePath()
);
}
}