Skip to content

Commit b1bd1a7

Browse files
committed
New method added to get the base url
1 parent 7c0a70e commit b1bd1a7

File tree

4 files changed

+63
-31
lines changed

4 files changed

+63
-31
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 1.1.0 - 2017-02-28
4+
* Added `Josantonius\Url\Url::getBaseUrl()` method.
5+
6+
## 1.1.0 - 2017-02-28
7+
* Added `Josantonius\Url\Tests\UrlTest::testGetBaseUrl()` method.
8+
39
## 1.0.0 - 2017-02-02
410
* Added `Josantonius\Url\Url` class.
511
* Added `Josantonius\Url\Url::getCurrentPage()` method.

src/Exception/UrlException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @copyright Copyright (c) 2017 JST PHP Framework
77
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
88
* @link https://github.com/Josantonius/PHP-Url
9-
* @since File available since 1.0.0 - Update: 2017-02-14
9+
* @since 1.0.0
1010
*/
1111

1212
namespace Josantonius\Url\Exception;
@@ -37,4 +37,4 @@ public function __construct($msg = '', $error = 0, $status = 0) {
3737
$this->code = $error;
3838
$this->statusCode = $status;
3939
}
40-
}
40+
}

src/Url.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @copyright Copyright (c) 2017 JST PHP Framework
88
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
99
* @link https://github.com/Josantonius/PHP-Url
10-
* @since File available since 1.0.0 - Update: 2017-02-14
10+
* @since 1.0.0
1111
*/
1212

1313
namespace Josantonius\Url;
@@ -30,17 +30,33 @@ class Url {
3030
*/
3131
public static function getCurrentPage() {
3232

33-
$protocol = static::getProtocol();
33+
$protocol = static::getProtocol();
3434

35-
$host = static::getDomain();
35+
$host = static::getDomain();
3636

37-
$port = ':' . static::getPort();
37+
$port = ':' . static::getPort();
3838

39-
$port = (($port == ':80') || ($port == ':443')) ? '' : $port;
39+
$port = (($port == ':80') || ($port == ':443')) ? '' : $port;
4040

41-
$uri = static::getUri();
41+
$uri = static::getUri();
4242

43-
return $protocol . '://' . $host . $port . $uri;
43+
return $protocol . '://' . $host . $port . $uri;
44+
}
45+
46+
/**
47+
* Get base url of the site.
48+
*
49+
* @since 1.1.0
50+
*
51+
* @return string → url
52+
*/
53+
public static function getBaseUrl() {
54+
55+
$uri = static::getUriMethods();
56+
57+
$url = trim(str_replace($uri, '', static::getCurrentPage()), '/');
58+
59+
return static::addBackslash($url);
4460
}
4561

4662
/**
@@ -52,13 +68,13 @@ public static function getCurrentPage() {
5268
*/
5369
public static function getProtocol() {
5470

55-
$protocol = strtolower($_SERVER['SERVER_PROTOCOL']);
71+
$protocol = strtolower($_SERVER['SERVER_PROTOCOL']);
5672

57-
$protocol = substr($protocol, 0, strpos($protocol, '/'));
73+
$protocol = substr($protocol, 0, strpos($protocol, '/'));
5874

59-
$ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
75+
$ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
6076

61-
return ($ssl) ? $protocol . 's' : $protocol;
77+
return ($ssl) ? $protocol . 's' : $protocol;
6278
}
6379

6480
/**
@@ -70,7 +86,7 @@ public static function getProtocol() {
7086
*/
7187
public static function isSSL() {
7288

73-
return (static::getProtocol() === 'https');
89+
return (static::getProtocol() === 'https');
7490
}
7591

7692
/**
@@ -82,7 +98,7 @@ public static function isSSL() {
8298
*/
8399
public static function getDomain() {
84100

85-
return $_SERVER['SERVER_NAME'];
101+
return $_SERVER['SERVER_NAME'];
86102
}
87103

88104
/**
@@ -120,7 +136,7 @@ public static function getUriMethods() {
120136
*/
121137
public static function getPort() {
122138

123-
return $_SERVER['SERVER_PORT'];
139+
return $_SERVER['SERVER_PORT'];
124140
}
125141

126142
/**
@@ -221,7 +237,7 @@ public static function generateSafeSlug($slug) {
221237
*/
222238
public static function segment($uri = null) {
223239

224-
$uri = (!is_null($uri)) ? $uri : $_SERVER['REQUEST_URI'];
240+
$uri = (!is_null($uri)) ? $uri : $_SERVER['REQUEST_URI'];
225241

226242
return explode('/', trim($uri, '/'));
227243
}
@@ -249,4 +265,4 @@ public static function getLastSegment($segments) {
249265

250266
return end($segments);
251267
}
252-
}
268+
}

tests/UrlTest.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<?php
1+
<?php
22
/**
33
* Library for urls manipulation.
44
*
55
* @author Josantonius - [email protected]
66
* @copyright Copyright (c) 2017 JST PHP Framework
77
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
88
* @link https://github.com/Josantonius/PHP-Url
9-
* @since File available since 1.0.0 - Update: 2017-02-14
9+
* @since 1.0.0
1010
*/
1111

1212
namespace Josantonius\Url\Tests;
@@ -27,7 +27,17 @@ class UrlTest {
2727
*/
2828
public static function testGetCurrentPage() {
2929

30-
echo '<pre>'; var_dump(Url::getCurrentPage()); echo '</pre>';
30+
echo '<pre>'; var_dump(Url::getCurrentPage()); echo '</pre>';
31+
}
32+
33+
/**
34+
* Get base url of the site.
35+
*
36+
* @since 1.0.0
37+
*/
38+
public static function testGetBaseUrl() {
39+
40+
echo '<pre>'; var_dump(Url::getBaseUrl()); echo '</pre>';
3141
}
3242

3343
/**
@@ -37,7 +47,7 @@ public static function testGetCurrentPage() {
3747
*/
3848
public static function testGetProtocol() {
3949

40-
echo '<pre>'; var_dump(Url::getProtocol()); echo '</pre>';
50+
echo '<pre>'; var_dump(Url::getProtocol()); echo '</pre>';
4151
}
4252

4353
/**
@@ -47,7 +57,7 @@ public static function testGetProtocol() {
4757
*/
4858
public static function testIsSSL() {
4959

50-
echo '<pre>'; var_dump(Url::isSSL()); echo '</pre>';
60+
echo '<pre>'; var_dump(Url::isSSL()); echo '</pre>';
5161
}
5262

5363
/**
@@ -57,7 +67,7 @@ public static function testIsSSL() {
5767
*/
5868
public static function getDomain() {
5969

60-
echo '<pre>'; var_dump(Url::testGetProtocol()); echo '</pre>';
70+
echo '<pre>'; var_dump(Url::testGetProtocol()); echo '</pre>';
6171
}
6272

6373
/**
@@ -67,7 +77,7 @@ public static function getDomain() {
6777
*/
6878
public static function testGetUri() {
6979

70-
echo '<pre>'; var_dump(Url::getUri()); echo '</pre>';
80+
echo '<pre>'; var_dump(Url::getUri()); echo '</pre>';
7181
}
7282

7383
/**
@@ -87,7 +97,7 @@ public static function testGetUriMethods() {
8797
*/
8898
public static function testGetPort() {
8999

90-
echo '<pre>'; var_dump(Url::getPort()); echo '</pre>';
100+
echo '<pre>'; var_dump(Url::getPort()); echo '</pre>';
91101
}
92102

93103
/**
@@ -97,7 +107,7 @@ public static function testGetPort() {
97107
*/
98108
public static function testAddBackslash() {
99109

100-
echo '<pre>'; var_dump(Url::addBackslash('path/to')); echo '</pre>';
110+
echo '<pre>'; var_dump(Url::addBackslash('path/to')); echo '</pre>';
101111
}
102112

103113
/**
@@ -107,7 +117,7 @@ public static function testAddBackslash() {
107117
*/
108118
public static function testPrevious() {
109119

110-
Url::previous();
120+
Url::previous();
111121
}
112122

113123
/**
@@ -172,7 +182,7 @@ public static function testSegment() {
172182
*/
173183
public static function testGetFirstSegment() {
174184

175-
$segments = Url::segment('path/to/panel/user');
185+
$segments = Url::segment('path/to/panel/user');
176186

177187
echo '<pre>'; var_dump(Url::getFirstSegment($segments)); echo '</pre>';
178188
}
@@ -184,8 +194,8 @@ public static function testGetFirstSegment() {
184194
*/
185195
public static function testGetLastSegment() {
186196

187-
$segments = Url::segment('path/to/panel/user');
197+
$segments = Url::segment('path/to/panel/user');
188198

189199
echo '<pre>'; var_dump(Url::getLastSegment($segments)); echo '</pre>';
190200
}
191-
}
201+
}

0 commit comments

Comments
 (0)