In this episode, we dive into the (previously painful) world of URL parsing and manipulation in PHP, and check out Laravel's shiny new URI helper that makes things a whole lot easier.
We start by looking at the old way—using PHP's built-in functions to parse a URL into its parts and dealing with the pain points: arrays that aren't null-safe, query strings you need to split up yourself, and just generally more work than it should be.
Then, we bring in Laravel's new URI helper. With it, you get a nice URI object from a URL string, with plenty of handy methods to grab out the scheme, host, port, query, and more. It's all null-safe, and you can really easily work with the query string as if it were a Laravel collection—super handy for pulling out specific query parameters or all of them at once.
But it's not just about parsing! We also look at how you can build URLs from scratch or modify existing ones really fluently. This means you can start with an empty URI object and just tack on each part you want (host, scheme, query, fragment, etc.) using method chaining. Or, if you receive a URL from somewhere and need to tweak it (like forcing HTTPS), that's super simple too.
We finish by checking out how all this integrates into Laravel's own request handling and routing—you can easily grab the current request's URI, tweak it as needed, or use helpers to build redirects and route URLs with all the correct parameters and fragments, without any painful string concatenation.
Overall, you'll see that handling URLs in Laravel is now much more convenient, reliable, and less error-prone, whether you're building them up or taking them apart.