Member-only story

Mastering URI Manipulation in Laravel: Simplify Routes and Queries

I Nyoman Jyotisa
3 min readJan 28, 2025

--

Laravel 11.35 introduced the powerful Uri class, leveraging the robust PHP League's URI library to make URI manipulation seamless. Whether you're working with named routes, query strings, or the current request, the Uri class offers a versatile API for simplifying how you interact with URIs in your Laravel application.

This guide will walk you through the key features of the Uri class, helping you unlock its full potential for your web development needs.

Effortless URI Manipulation

At the core of the Uri class lies its ability to create and manipulate URI strings effortlessly. You can adjust paths, query parameters, and fragments with minimal code:

use Illuminate\Support\Uri;  

$uri = Uri::of('https://laravel.test')
->withPath('links')
->withQuery(['page' => 2])
->withFragment('new');

echo (string) $uri; // Outputs: https://laravel.test/links?page=2#new

// Access individual URI components
echo $uri->path(); // links
echo $uri->scheme(); // https
echo $uri->host(); // laravel.test

Not only can you build URIs, but you can also extract and interact with specific components such as the path, scheme, or host. This ensures that your URI handling is both dynamic and maintainable.

--

--

No responses yet