Native URI manipulation in Laravel
A URI (Uniform Resource Identifier) is a string of characters that uniquely identifies a resource on the internet or a local network. It provides a way to locate or reference resources such as web pages, files, images, or services.
Understanding how URIs work allows developers to debug issues efficiently, particularly when dealing with routing, handling application logic like switching between different views, states, or resources, or security-related issues such as unauthorized access, information leakage, or server misconfigurations.
A typical URI is composed of the following components:
- Scheme — Identifies the protocol used (e.g., http, ftp, mailto).
- Authority — Includes domain or IP address and sometimes port number.
- Path — Specifies the location of the resource.
- Query (optional) — Contains parameters for dynamic resources, usually as key-value pairs.
- Fragment (optional) — Refers to a specific section of the resource.
When you want to understand a URL better, the most important way is to break it down into its components and manipulate them wherever needed.
In the recent Laravel release, the native support to parse and manipulate URIs has landed in the Illuminate\Support\Uri
class. The class uses the league/uri package to provide the parsing and manipulation functionality under the hood.
The class lets you parse/manipulate a URI for:
Third-party URLs
So, if you want to build and parse a third-party URL using the Illuminate\Support\Uri
class, here’s how you can do it.
use Illuminate\Support\Uri;
$uri = Uri::of('https://amitmerchant.com')
->withQuery(['q' => 'laravel'])
->withPath('/search');
As you can tell, the class provides a bunch of methods that you can specify different components of a URL. And there are various methods to get back the components as well.
So, for instance, if you want the string form of this URL, you can get it like so.
$uri->value();
// https://amitmerchant.com/search?q=laravel
You can also get the scheme, host, port, path, query, and fragment components of the URL.
$uri->scheme();
// https
$uri->host();
// amitmerchant.com
$uri->port();
// null
$uri->path();
// /search
$uri->query()->all();
// ['q' => 'laravel']
If the URL contains fragments, they can also be handled pretty conveniently.
$uri = Uri::of('https://amitmerchant.com')
->withFragment('techstack')
->withPath('/uses');
$uri->fragment();
// #techstack
Any URL path of the current app
As I said, URI manipulation can also be used with current application URLs as well by specifying the given path of an absolute URL.
$uri = Uri::to('/documentation')->withQuery(['search' => 'laravel']);
You can play with this URI instance the same way shown previously.
Named routes
Named routes can also be parsed and manipulated using the Uri::route()
method like so.
$uri = Uri::route('named-route', ['user' => $user]);
Current request
Lastly, you can also retrieve the URI instance from the current request like so.
use Illuminate\Http\Request;
Route::get('/uri', function (Request $request) {
$uri = $request->uri();
dump($uri->value());
});
You can refer to this PR to learn more about this feature.
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.