1212namespace CodeIgniter \HTTP ;
1313
1414use CodeIgniter \HTTP \IncomingRequest ;
15+ use CodeIgniter \Router \Exceptions \RouterException ;
1516use Config \App ;
1617use Config \Services ;
1718use InvalidArgumentException ;
2021 * URI wrapper to work with complete project URLs.
2122 * This class creates immutable instances.
2223 */
23- class URL
24+ final class URL
2425{
2526 /**
2627 * Underlying URI instance
@@ -34,18 +35,29 @@ class URL
3435 *
3536 * @var string
3637 */
37- protected $ relativePath ;
38+ private $ relativePath ;
3839
3940 //--------------------------------------------------------------------
4041
4142 /**
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')
4355 *
4456 * @param string $uri Additional URI string to include
4557 *
4658 * @return static
4759 */
48- final public static function base (string $ uri = '' )
60+ public static function public (string $ uri )
4961 {
5062 // Base URLs never include the index page
5163 $ config = clone config ('App ' );
@@ -55,33 +67,45 @@ final public static function base(string $uri = '')
5567 }
5668
5769 /**
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.
6171 *
6272 * @return static
6373 */
64- final public static function to ( string $ uri )
74+ public static function current ( )
6575 {
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+ }
7378
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 , '/ ' ));
7589 }
7690
7791 /**
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
7995 *
8096 * @return static
97+ *
98+ * @throws RouterException
8199 */
82- final public static function current ( )
100+ public static function route ( string $ uri )
83101 {
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 );
85109 }
86110
87111 /**
@@ -92,7 +116,7 @@ final public static function current()
92116 *
93117 * @return static
94118 */
95- final public static function fromRequest (IncomingRequest $ request )
119+ public static function fromRequest (IncomingRequest $ request )
96120 {
97121 $ path = $ request ->detectPath ($ request ->config ->uriProtocol );
98122 $ query = isset ($ _SERVER ['QUERY_STRING ' ]) ? '? ' . $ _SERVER ['QUERY_STRING ' ] : '' ;
@@ -109,7 +133,7 @@ final public static function fromRequest(IncomingRequest $request)
109133 * @param string $relativePath
110134 * @param App|null $config
111135 */
112- final public function __construct (string $ relativePath = '' , App $ config = null )
136+ public function __construct (string $ relativePath = '' , App $ config = null )
113137 {
114138 $ config = $ config ?? config ('App ' );
115139
@@ -175,11 +199,14 @@ public function getUri(): URI
175199
176200 /**
177201 * 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().
178205 *
179206 * @return string
180207 */
181208 public function __toString (): string
182209 {
183- return (string ) $ this ->uri ;
210+ return (string ) $ this ->getUri ()-> setQuery ( '' ) ;
184211 }
185212}
0 commit comments