12
12
namespace CodeIgniter \HTTP ;
13
13
14
14
use CodeIgniter \HTTP \IncomingRequest ;
15
+ use CodeIgniter \Router \Exceptions \RouterException ;
15
16
use Config \App ;
16
17
use Config \Services ;
17
18
use InvalidArgumentException ;
20
21
* URI wrapper to work with complete project URLs.
21
22
* This class creates immutable instances.
22
23
*/
23
- class URL
24
+ final class URL
24
25
{
25
26
/**
26
27
* Underlying URI instance
@@ -34,18 +35,29 @@ class URL
34
35
*
35
36
* @var string
36
37
*/
37
- protected $ relativePath ;
38
+ private $ relativePath ;
38
39
39
40
//--------------------------------------------------------------------
40
41
41
42
/**
42
- * Returns an instance representing the base URL.
43
+ * Creates the base URL.
44
+ *
45
+ * @return static
46
+ */
47
+ public static function base ()
48
+ {
49
+ return static ::public ('' );
50
+ }
51
+
52
+ /**
53
+ * Creates a URL to unrouted public files (typically assets).
54
+ * Similar to base_url('path/to/file')
43
55
*
44
56
* @param string $uri Additional URI string to include
45
57
*
46
58
* @return static
47
59
*/
48
- final public static function base (string $ uri = '' )
60
+ public static function public (string $ uri )
49
61
{
50
62
// Base URLs never include the index page
51
63
$ config = clone config ('App ' );
@@ -55,33 +67,45 @@ final public static function base(string $uri = '')
55
67
}
56
68
57
69
/**
58
- * Returns an instance representing a routed URL.
59
- *
60
- * @param string $uri Named route, reverse route, or URI string
70
+ * Returns an instance representing the current URL.
61
71
*
62
72
* @return static
63
73
*/
64
- final public static function to ( string $ uri )
74
+ public static function current ( )
65
75
{
66
- $ uri = rtrim ($ uri , '/ ' );
67
-
68
- // Check for a named or reverse-route
69
- if ($ uri !== '' && $ route = Services::routes ()->reverseRoute ($ uri ))
70
- {
71
- return new static ($ route );
72
- }
76
+ return static ::fromRequest (Services::request ());
77
+ }
73
78
74
- return new static ($ uri );
79
+ /**
80
+ * Creates a framework URL.
81
+ *
82
+ * @param string $uri
83
+ *
84
+ * @return static
85
+ */
86
+ public static function to (string $ uri )
87
+ {
88
+ return new static (rtrim ($ uri , '/ ' ));
75
89
}
76
90
77
91
/**
78
- * Returns an instance representing the current URL.
92
+ * Creates a URL to a named or reverse route.
93
+ *
94
+ * @param string $uri Named or reverse route
79
95
*
80
96
* @return static
97
+ *
98
+ * @throws RouterException
81
99
*/
82
- final public static function current ( )
100
+ public static function route ( string $ uri )
83
101
{
84
- return static ::fromRequest (Services::request ());
102
+ // Check for a named or reverse-route
103
+ if ($ route = Services::routes ()->reverseRoute ($ uri ))
104
+ {
105
+ return new static ($ route );
106
+ }
107
+
108
+ throw RouterException::forInvalidRoute ($ uri );
85
109
}
86
110
87
111
/**
@@ -92,7 +116,7 @@ final public static function current()
92
116
*
93
117
* @return static
94
118
*/
95
- final public static function fromRequest (IncomingRequest $ request )
119
+ public static function fromRequest (IncomingRequest $ request )
96
120
{
97
121
$ path = $ request ->detectPath ($ request ->config ->uriProtocol );
98
122
$ query = isset ($ _SERVER ['QUERY_STRING ' ]) ? '? ' . $ _SERVER ['QUERY_STRING ' ] : '' ;
@@ -109,7 +133,7 @@ final public static function fromRequest(IncomingRequest $request)
109
133
* @param string $relativePath
110
134
* @param App|null $config
111
135
*/
112
- final public function __construct (string $ relativePath = '' , App $ config = null )
136
+ public function __construct (string $ relativePath = '' , App $ config = null )
113
137
{
114
138
$ config = $ config ?? config ('App ' );
115
139
@@ -175,11 +199,14 @@ public function getUri(): URI
175
199
176
200
/**
177
201
* Returns this URL as a string.
202
+ * Since this is typically for routing and
203
+ * link purposes we strip any queries, but
204
+ * they can be accessed via getUri().
178
205
*
179
206
* @return string
180
207
*/
181
208
public function __toString (): string
182
209
{
183
- return (string ) $ this ->uri ;
210
+ return (string ) $ this ->getUri ()-> setQuery ( '' ) ;
184
211
}
185
212
}
0 commit comments