Playing
16. URI Parsing and Mutation

Transcript

00:00
We all know that parsing, manipulating and building URLs up in PHP can be a bit of a nightmare. Laravel's introduced a new URI helper which allows us to do just that really fluently.
00:13
Okay so first of all let's take a look at how we would just do this in raw PHP and then we'll dive in and see the difference between that and this new helper. Okay so let's go ahead and build up a URI here. Now let's go and just choose HTTPS and Cocourse.com here as an example. We could even add on to the end of this something in the query string. Now to go ahead and parse this e.g. pluck
00:40
out parts of this that we need in PHP we would use the parse URL function. Let's go ahead and die dump on that and see the result that we get over in the browser and you can see here sure enough we've got the scheme the host and the query so everything has been nicely broken up for us and of course we can go ahead and extract out any parts of this that we need. Now the problem is
01:03
first of all this is just a standard array so it's not null safe. If for example the query was missing and we were trying to access it we would just get an error. Another part of this is that even if we did have the query and we had multiple query elements to this it would still be really difficult to dive in and build this up. So for example let's go ahead and say category and Laravel
01:26
and let's head over and give that refresh. You can see we pretty much just get the string here and we would have to rely on further functions or tools to break this up so we could go ahead and read each individual part of this query string. The same thing for fragments as well so let's go ahead and create our fragment here for a section and give that a refresh. You can see we do get
01:47
the fragment but again this isn't an ideal solution. Okay so what's changed then within Laravel that now allows us to manipulate, build up and parse URLs? Well let's take the same example that we just looked at so let's actually bring this back again and we'll just take this entire example here. We now have the use of a URI helper so we pull this in from illuminate support URI.
02:13
Now there's a bunch of ways that we can use this but we're going to start with the of method which takes in the URL that we want to read. Let's go ahead and die dump this and see the difference that we get here. Okay so obviously we're not just going to get an array with these things broken up because that's just the standard PHP way of doing things. Instead we get this URI object which does
02:35
contain all of this data so it's pretty much exactly the same thing but we now have a bunch of useful methods and in particular we're going to focus on this query part as well. Okay so what can we do with this new URI object? Well we can use any of the methods here like scheme, host, port to grab any of the details about this URL. This is null safe as well so for example if we didn't
03:02
have anything in the query string and we wanted to try and grab the query data this of course is going to return to us something else other than just erroring out because we're not doing something like the following. We're not saying URI and query to access this out so this is all being checked behind the scenes. Okay let's bring this back to the query string and the fragment here and let's
03:25
look at what else we can do here. We can grab out the host here which will give us obviously codecourse.com. We can grab out the scheme here as well which gives us HTTPS and we can do multiple other things here as well. Before we go any further it's probably a good idea if you just dive into the URI helper so just open this up in your text editor and go ahead and look through any of
03:47
the methods that are contained within here because you'll find that directly within the source. It's much more convenient to just look straight into the source. Okay now let's focus on the query method here because we get a slightly different result here. This returns to us a URI query string object. Now what this allows us to do is if we just open this up so URI query string here,
04:14
this allows us to pretty much work with this like a Laravel collection. So we've got things like all, we've got things like get. So what we could do now is say things like all and if we give that a refresh we get an array with this data in so we can use that in some way. But if we just wanted to pluck one thing out so for example if we were just interested in the page within the query string
04:38
then sure enough we get that value back here as well and even if that didn't exist again it is null safe so if we give that a refresh we just get a null value back instead of an error. So already just for reading and parsing a URL and extracting parts out of this URL or URI this is a lot more convenient. Now there's another side to this builder so we've taken a look at parsing this
05:03
but let's go ahead and take a look at building up a URL which in traditional sense would mean that we would have to start with a string go ahead and add on a scheme in here then go ahead and concatenate on the host and then of course we would have to build that array up possibly flatten it down to build up the query string. Now with the URI builder we don't need to do any of that and
05:26
there's a few different ways that you can use this let's look at the most basic way of using this so that would be to create out a new URI instance what we can do from here is continue to chain on any of these methods to build this up so instead of accessing the host the scheme the query or the fragment or any other part of the URL instead we say with host so we can go ahead and
05:51
start with pretty much just an empty URI object and then go ahead and just add in the bits that we need so this is really helpful if for whatever reason in your app you need to build up a URL and go ahead and then present it to the user or store it whatever you need to do. Okay let's go ahead and die dump on this so let's die dump that URI and sure enough if we come over
06:12
once again we've got this built up with the correct parts of this what we can actually do is we can go ahead and cast this to a string so if we use a cast on this and give it a refresh there we go we've got the fully built up URL and again you can do things like with query so we know that a query string works by having a question mark at the end of the URL and then it has all of the values
06:34
separated by ampersand so we don't need to do that now we can just pass in an array of all of the values that we want in here so let's go with the original example here with the category as laravel the page is one and there we go everything is built up for us and of course we've also got things like with fragment typically less used but we've got everything that we need here to pretty
06:57
much build up any URL that we need and more importantly do this fluently as well now with this in mind another thing that we can do is we can already start with a URL so let's say for example you had an application that took a URL that existed but you need to modify it in some way well let's go ahead and use the URI helper but let's not new this up let's say URI of and then
07:21
just do exactly what we did before so let's say http.cocourse.com let's say as part of a form that you are allowing users to submit URLs to you always want us to change the scheme over to HTTPS well you can now easily do that you can say with scheme HTTPS and whatever is provided here or even if it hasn't been provided you're always now going to end up if we just cast this to a string
07:50
you're always going to end up with a secure version of whatever has been passed down now this also contains some other methods that you probably wouldn't use day to day let's take a look at them as well so let's go ahead and comment this out and as an example let's take a look at dynandumping URI of let's take an FTP address here obviously that's a valid scheme with a username
08:13
and a password this is typically how we would authenticate with a insecure FTP server on the back of this you can go ahead and grab out the user which in this case is Alex we've got that here just before the colon and we can also extract the password as well just here so again imagine you had an application where you allow the user to enter a URL like this you could very easily
08:36
extract this data out to go ahead and authenticate with an FTP server if you needed to okay so we've pretty much covered all the basics you can of course dive into that object go ahead and look through the list of methods here I'm sure more will be added as we go and you can go ahead and use that for whatever you need let's now talk though about how this integrates into Laravel
08:56
on a slightly deeper level so for example we've got our request either within our route that we're working with here or within a controller well what we can now do is say URI now that will give us the current URI of the page that we are on so if we give that a refresh this now represents the page that we're on and of course I have just the project name here as I'm showing you so again this just
09:22
returns a URI object so again what we can do is we can either build this further or we can extract out information that we need so for example if you wanted to get the page number you could say query get and then page of course that doesn't exist at the moment so we just get null but if we were to add page of one onto there then sure enough you can see we get that value you kind of get the
09:44
idea because this returns a URI object you can continue to either modify this URL with the width so for example you could change this to an insecure version of the URL that you're working with and if we just go ahead and cast this to a string we'll see that in action there we go so we've got an HTTP rather than an HTTPS if that is something that you wanted to do you kind of get the idea
10:10
now another place that we can use this is to build up URLs within our application either by the URL that we want this to point to or through the root name let's take a look at an example so let's say we were building up a redirect to another page we could go ahead and say URI2 now we don't need to provide the full URL of our application in here with the scheme the host we could just say well
10:36
let's go ahead and build up a URL to the home page but let's include a query now I'm sure if you've been working with Laravel for a while you'll know that typically what you would do in this example is you would use something like root you would pass in either the root name or the URL into just a string then you would have to go ahead and concatenate on the page number like this as an
11:02
example and obviously if things get more complicated this is now a really great solution so if we wanted to build up a URL to the home page of our application we just say URI2 we give the location and if we just die dump on this redirect here that's going to go ahead and build up for us and once again if we just cast this to a string there we go so we now have this included in the URL so
11:28
what you can now do is go ahead and return redirect the user to that redirect and you've fluently built this up rather than having to concatenate on or perform any additional checks now I'm not actually going to redirect the user because we're already on the home page here but let's take a look at another example so I don't have any roots in this application at the moment let's say we had a
11:51
dashboard page so let's build out a dashboard in here we'll just create a simple closure that returns a string just to demo this and let's go ahead and give this a name of dashboard what you can also do instead of URI2 is you can say URI and root that's going to go ahead and take a named root within your application it's going to fetch the URL that this belongs to in this case it's
12:16
slash dashboard and then you can go ahead and continue to chain on any of these with methods to build this up so again let's go ahead and just die dump on the string version of this redirect and let's see what we get and there we go we've built up a URL to the dashboard including the page so you can just continue to do this so again you could say something like with scheme and you
12:36
could set this to specifically be secure if you wanted to or you could go ahead and say with fragment and you could point to a specific section on this page when you redirected anything that you need to do the point is it's now a lot more fluent to build these up okay so that pretty much covers and wraps up the new URI helper as you can see this is baked into the
12:58
framework in a few ways which makes it really nice and easy to either just build up a URL from a string build up a URL from nothing redirect to a specific page with additional things added to the URL or redirect to a named root with these modifications however you are either parsing or manipulating URLs this is going to be super helpful
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!