|
10 | 10 | - [Pipeline](#pipeline)
|
11 | 11 | - [Sleep](#sleep)
|
12 | 12 | - [Timebox](#timebox)
|
| 13 | + - [URI](#uri) |
13 | 14 |
|
14 | 15 | <a name="introduction"></a>
|
15 | 16 | ## Introduction
|
@@ -3261,3 +3262,107 @@ use Illuminate\Support\Timebox;
|
3261 | 3262 | ```
|
3262 | 3263 |
|
3263 | 3264 | If an exception is thrown within the closure, this class will respect the defined delay and re-throw the exception after the delay.
|
| 3265 | + |
| 3266 | +<a name="uri"></a> |
| 3267 | +### URI |
| 3268 | + |
| 3269 | +Laravel's `Uri` class provides a convenient and fluent interface for creating and manipulating URIs. This class wraps the functionality provided by the underlying League URI package and integrates seamlessly with Laravel's routing system. |
| 3270 | + |
| 3271 | +You can create a `Uri` instance easily using static methods: |
| 3272 | + |
| 3273 | +```php |
| 3274 | +use App\Http\Controllers\UserController; |
| 3275 | +use App\Http\Controllers\InvokableController; |
| 3276 | +use Illuminate\Support\Uri; |
| 3277 | + |
| 3278 | +// Generate a URI instance from the given string... |
| 3279 | +$uri = Uri::of('https://example.com/path'); |
| 3280 | + |
| 3281 | +// Generate URI instances to paths, named routes, or controller actions... |
| 3282 | +$uri = Uri::to('/dashboard'); |
| 3283 | +$uri = Uri::route('user.profile', ['user' => 1]); |
| 3284 | +$uri = Uri::action([UserController::class, 'index']); |
| 3285 | +$uri = Uri::action(InvokableController::class); |
| 3286 | +``` |
| 3287 | + |
| 3288 | +Once you have a URI instance, you can fluently modify it: |
| 3289 | + |
| 3290 | +```php |
| 3291 | +$uri = Uri::of('https://example.com') |
| 3292 | + ->withScheme('http') |
| 3293 | + ->withHost('test.com') |
| 3294 | + ->withPort(8000) |
| 3295 | + ->withPath('/users') |
| 3296 | + ->withQuery(['page' => 2]) |
| 3297 | + ->withFragment('section-1'); |
| 3298 | +``` |
| 3299 | + |
| 3300 | +<a name="inspecting-uris"></a> |
| 3301 | +#### Inspecting URIs |
| 3302 | + |
| 3303 | +The `Uri` class also allows you to easily inspect the various components of the underlying URI: |
| 3304 | + |
| 3305 | +```php |
| 3306 | +$scheme = $uri->scheme(); |
| 3307 | +$host = $uri->host(); |
| 3308 | +$port = $uri->port(); |
| 3309 | +$path = $uri->path(); |
| 3310 | +$query = $uri->query(); |
| 3311 | +$fragment = $uri->fragment(); |
| 3312 | +``` |
| 3313 | + |
| 3314 | +<a name="manipulating-query-strings"></a> |
| 3315 | +#### Manipulating Query Strings |
| 3316 | + |
| 3317 | +The `Uri` class offers several methods that may be used to manipulate a URI's query string. The `withQuery` method may be used to merge additional query string parameters into the existing query string: |
| 3318 | + |
| 3319 | +```php |
| 3320 | +$uri = $uri->withQuery(['sort' => 'name']); |
| 3321 | +``` |
| 3322 | + |
| 3323 | +The `withQueryIfMissing` method may be used to merge additional query string parameters into the existing query string if the given keys do not already exist in the query string: |
| 3324 | + |
| 3325 | +```php |
| 3326 | +$uri = $uri->withQueryIfMissing(['page' => 1]); |
| 3327 | +``` |
| 3328 | + |
| 3329 | +The `replaceQuery` method may be used to complete replace the existing query string with a new one: |
| 3330 | + |
| 3331 | +```php |
| 3332 | +$uri = $uri->replaceQuery(['page' => 1]); |
| 3333 | +``` |
| 3334 | + |
| 3335 | +The `pushOntoQuery` method may be used to push additional parameters onto a query string parameter that has an array value: |
| 3336 | + |
| 3337 | +```php |
| 3338 | +$uri = $uri->pushOntoQuery('filter', ['active', 'pending']); |
| 3339 | +``` |
| 3340 | + |
| 3341 | +The `withoutQuery` method may be used to remove parameters from the query string: |
| 3342 | + |
| 3343 | +```php |
| 3344 | +$uri = $uri->withoutQuery(['page']); |
| 3345 | +``` |
| 3346 | + |
| 3347 | +<a name="generating-responses-from-uris"></a> |
| 3348 | +#### Generating Responses From URIs |
| 3349 | + |
| 3350 | +The `redirect` method may be used to generate a `RedirectResponse` instance to the given URI: |
| 3351 | + |
| 3352 | +```php |
| 3353 | +$uri = Uri::of('https://example.com'); |
| 3354 | + |
| 3355 | +return $uri->redirect(); |
| 3356 | +``` |
| 3357 | + |
| 3358 | +Or, you may simply return the `Uri` instance from a route or controller action, which will automatically generate a redirect response to the returned URI: |
| 3359 | + |
| 3360 | +```php |
| 3361 | +use Illuminate\Support\Facades\Route; |
| 3362 | +use Illuminate\Support\Uri; |
| 3363 | + |
| 3364 | +Route::get('/redirect', function () { |
| 3365 | + return Uri::to('/index') |
| 3366 | + ->withQuery(['sort' => 'name']); |
| 3367 | +}); |
| 3368 | +``` |
0 commit comments