‫Okay.
‫So this is going to be kind of a tough video because there's quite a bit we have to do to the router,
‫especially in the route method, because what I want to be able to do is instead of having to go to
‫listing and then adding ID equals whatever as a query string, we want to be able to just say listing
‫slash and then whatever the ID is.
‫All right.
‫So to do that, there's a little bit of work we have to do in the router.
‫So yes we have another router refactor.
‫So let's open up router dot PHP.
‫And we're really only going to be working in the route method.
‫So this right here because again we want to be able to accept those params.
‫We want to be able to in the routes.
‫Right.
‫So instead of just saying listing and then having the show and then passing the ID in through a get
‫variable, instead of doing that, I want to say listing slash and then add a parameter by using these
‫curly braces and then whatever I want that the name of that parameter to be in this case id okay.
‫So and this is how it's done in Laravel.
‫This is how it's done in most frameworks.
‫You might see like for instance with express it'll be colon ID instead.
‫But we're going to do the stick to the Laravel convention and do parentheses or curly braces.
‫All right.
‫So now if I go back here to our details or show page it's not going to work.
‫Right.
‫Because now this isn't matching listing isn't matching any route.
‫It's this listing ID and it has no idea what to do with this.
‫So we need to account for this in our route method in the router.
‫So the first thing I'm going to do is instead of in our public index PHP.
‫So public index PHP what we've been doing is getting the Uri I'm sorry getting the method from here.
‫In this file and then passing it in to the root.
‫Well, I want to change that.
‫I want to actually get the method within the router.
‫And there's a reason for that, mainly because later on when we when we delete a listing or we update
‫a listing, we want to be able to have it mimic a put or a delete request.
‫And we do that with a hidden file input.
‫And we need to match that against the method.
‫So this helped.
‫This will help us out later on.
‫So we can just take this and cut it and then not pass in the method.
‫Just pass in the Uri right?
‫And right now it gives us an error because it's expecting that method.
‫So let's go to the router and the route and then remove this param right here.
‫So now it's only expecting a Uri.
‫And then I'm going to go above the for each and just paste that in.
‫Except I'm going to change it to request method for the variable.
‫Okay.
‫So we'll say request method equals and then using the server Superglobal we'll get that method.
‫What we're going to do now let's close that up.
‫In this for each loop.
‫I do want this stuff here because we're going to want to obviously get the controller.
‫Then the method and instantiate that method.
‫And.
‫This here, this, this if statement.
‫I want to get rid of that for now.
‫So let's just actually no, we'll keep it.
‫But let's comment out everything in the for each because we want to go into this for each at the top
‫and then start to do our thing.
‫Now I want to be able to go to something like this listing slash two, slash one, whatever the ID is.
‫So we need to be able to get that value.
‫So what we'll do is we'll split the current route or the current Uri into segments, meaning we'll turn
‫this into an array so we can access that item.
‫So in the for each make sure you're inside the for each we're going to split the current.
‫Uri into segments.
‫And we'll do that using explode, which will turn a string into an array.
‫So let's create a variable called Uri segments.
‫Set that to equal explode.
‫And we're going to pass in where we want to break it or explode it, which is going to be the slash,
‫obviously.
‫And then the Uri, which I'm just going to wrap in, trim, not trim, trim and pass in the Uri and
‫also slash.
‫All right.
‫Now to see what that gives us.
‫Let's do an inspect and die and let's say Uri segments just so you can see exactly what we're getting.
‫So if I go to listing slash one and reload we now have an array with the first item is listing and the
‫second item is one.
‫If I go to slash two you'll see that.
‫Now this is two.
‫And we can get that specific value with the one index because it's the second item.
‫If I reload then we get to whatever is passed in here is going to be in this.
‫So that takes care of of segmenting the current Uri.
‫Now we want to segment the route that we're currently in in the for each.
‫So it's going to be very similar to this.
‫So what I'll do is copy that and I'll say split the.
‫Split the route.
‫I guess.
‫Yeah, it's the current route or the current iteration in the loop.
‫So split the route Uri into segments.
‫Okay.
‫Now we're going to do it the same way.
‫So let's change this to route segments.
‫And then instead of Uri it's going to be route.
‫You are, I like that.
‫All right.
‫And then I'll just go ahead and.
‫We can do.
‫Let's do an inspect because it's a loop.
‫So we want to be able to see all of them.
‫So I'll say inspect the.
‫Uh, what is it?
‫The root.
‫Route segments.
‫All right.
‫Oh.
‫Semicolon.
‫Let's reload.
‫Okay.
‫So now what you're seeing here is the segments for every route that we have defined in routes dot PHP.
‫So we have just well this one is just nothing because.
‫And there is no there's nothing after slash.
‫That's the home.
‫Then we have listings.
‫So that is this one.
‫Then we have listings and then create.
‫Right.
‫So we have an array with two items now because it's listing slash create.
‫And then we have listing and then ID.
‫So this would just getting the plate basically the placeholder okay.
‫Which is what we want.
‫We want to be able to get that placeholder.
‫And we want to be able to get this right here.
‫The actual ID.
‫So let's go back to our router.
‫And we're going to want to compare each segment.
‫And we'll start off by creating a value or variable of match.
‫And I'm going to set that to true.
‫Now there's a few checks that we have to run here.
‫So we're going to have quite a few if statements.
‫And we're also going to loop through the Uri segments.
‫And basically we need to check for params which look like this.
‫They have the the curly braces and then text in the middle.
‫So the first thing I want to check for is if the number of segments match for both of these, whatever
‫the Uri segments and route segments.
‫So let's say check.
‫Check if.
‫The number of segments.
‫Matches.
‫So let's say if and we're going to use count.
‫Here.
‫So let's say if count and let's count the Uri segments.
‫And we want to say if that is equal to the count.
‫Of the route segments.
‫And so double ampersand and we'll do string to upper.
‫And the method equals the request method.
‫So let's say if the root.
‫Methods so that whatever iteration we're on.
‫Because remember, we're looping through all the roots here.
‫So whatever the current iteration, does that equal the actual current request method?
‫All right.
‫So this represents the actual method that we're going to in the browser.
‫And this represents the current iteration of the routes.
‫Okay.
‫So we're just we're matching those and we're making sure that the number of segments match.
‫If that's true then we're going to first off create a params.
‫Oops no quotes.
‫We're going to first off create a params array.
‫Just an empty array.
‫And we are going to set match equal to true.
‫So basically it's just staying true.
‫And it'll continue.
‫And then we want to loop through the Uri segments.
‫So let's say for.
‫Set I to zero, and we'll say as long as I is less than the count of the Uri segments.
‫And then we'll increment I by one.
‫Okay.
‫Now in this for loop, I want to check to see if the if first of all, if the root segments match the
‫Uri segments.
‫And we want to check to see if there's a param, meaning we're going to use a regular expression to
‫see if there's something in there that's wrapped in curly braces, because that's a param.
‫So let's say if.
‫And we'll take root segments.
‫And then in brackets we're going to put whatever that AI variable is.
‫So the current iteration variable and say if that is not equal to Uri segments.
‫Current I variable and.
‫Oops.
‫And we're going to use preg underscore match which will match something to a regular expression.
‫Actually I'm sorry.
‫We want to say if not preg match.
‫So in pre-match, we need a regular expression that's going to look for text wrapped in curly braces.
‫Now, I hate regular expressions.
‫I'm not very good at them.
‫It's to me it's a separate it's separate from learning PHP because regular expressions are in just about
‫every language.
‫So I'm not going to really go over exactly what this regular expression does or how it works, but just
‫know it's looking for text inside of parentheses.
‫All right.
‫So we're saying if that doesn't match and Prague match takes in takes in a second argument.
‫So we're going to go after.
‫Let's see.
‫After the single quote right here.
‫And what we're matching that against is the route segments.
‫So route.
‫Segments and then whatever that eye variable is.
‫So if that doesn't match, then we're going to set that match.
‫That match variable is then going to equal false.
‫And we're just going to break from the function or from sorry from the loop.
‫So kind of as a summary this if statement I'll just put a comment here.
‫We're saying if the Uri's.
‫Do not match and there is no peram.
‫And when I say peram, I mean a value in between curly braces.
‫So that's what we're checking here.
‫These don't match and there's no peram then match is going to be false and then we break.
‫Now if it does match then we want to get that peram.
‫We want to figure out what that is.
‫So.
‫Let's say if pragmatic.
‫And in here we're going to put the same.
‫The same regular expression.
‫It's just checking for text wrapped in curly braces.
‫Okay.
‫And then as a second argument is what we want to match.
‫So in this case it'll be the root.
‫Segments.
‫And whatever that eye variable is.
‫And then the third, third argument is just going to be the variable that is going to be used to hold
‫those the matches.
‫And that's called matches.
‫Okay.
‫So let's.
‫Let's just see what that is.
‫Inspect and die and let's look at matches.
‫And then we'll reload this page.
‫This is listing slash to.
‫I'll reload.
‫And now you'll see that it actually has that curly brace ID in the array and also ID.
‫So that's what's in this matches.
‫So to get the actual params or to get the actual what's in the curly braces then we would do matches
‫one.
‫If I do that then we're seeing that it's ID.
‫Now that ID we want to, we want that to be the key in this params array.
‫Where is it that we initialized this params array?
‫We want that to be the key.
‫And then we want whatever that segment is to be stored in it.
‫Like let me let's inspect and I.
‫Uh.
‫Uri.
‫Segments and then whatever I is.
‫So if I look at that you'll see we get to.
‫So that's the value.
‫So again, we want this to be the value and we want.
‫This to be the key in that params.
‫So what we'll do is in this if statement, let's say params and then in brackets we're going to add
‫our matches and then the one index.
‫So that in this case would be ID.
‫And then we want that to equal the value which would be Uri segments.
‫And then the I variable.
‫Okay.
‫Now after that I'll just do an inspect and die of params.
‫Save that reload.
‫And now we have an ID pointing to two.
‫If I change this to five, we get five.
‫And if I were to in the roots change this to let's say name right.
‫And then I go to this page again.
‫Now it's name pointing to five.
‫So it's whatever I pass in here.
‫But of course I want ID.
‫So hopefully that makes sense.
‫This is some this is pretty tough.
‫You know this is getting there in terms of advanced code at least in my opinion.
‫So now at the end here after the for loop.
‫So the for loop ends right here.
‫And let me just put a comment here.
‫We'll say check check for the param.
‫And add to.
‫Params.
‫Array.
‫Get rid of that.
‫Inspect and die.
‫All right.
‫And then under the for loop, what we'll do is then test if match is still true.
‫So if match, then I'm going to take this stuff here.
‫And not the if statement.
‫Just this everything within IT controller controller method and then instantiating it.
‫I'm going to grab that.
‫Get rid of that and inside if match, we'll paste that in.
‫Let's uncomment it.
‫Okay.
‫And then.
‫Let's see.
‫The only thing that I want to add here is the controller method is now going to take in params.
‫Okay.
‫So that we can then have access to whatever that that params array is.
‫So if I reload this, we're going to get an error whenever it tries to access a value because it's still
‫looking for that get variable.
‫It's still looking for that query string.
‫So all we have to do is now go into our show and our listing controller.
‫And instead of getting it from get, now we're getting it from the params which are being passed in.
‫Here.
‫And we can change this to params.
‫Okay, so it'll just be params id and then down here this can be the same actually.
‫Probably want to know this should still work.
‫Yeah.
‫This should still work.
‫Let's try it out.
‫We'll save reload, and there we go.
‫So it's working.
‫And this is listing five.
‫If I go to listing one software engineer.
‫If I go to listing, let's say 20, which doesn't exist, we get this.
‫So we can handle that by coming down here.
‫And after we fetch the listing, let's say check if listing exists.
‫So if.
‫If not.
‫Listing.
‫So if not listing, then let's use our error controller.
‫And let's use not found.
‫And what I'll do is pass in a custom message, because if I don't, it'll just say resource not found.
‫But we know we're dealing with listings.
‫So let's say listing.
‫Not found.
‫And then after that, we're just going to return.
‫All right.
‫So let's go ahead and reload listing 20 which doesn't exist.
‫And we get listing not found.
‫All right.
‫So yeah.
‫And we should probably have a back button here.
‫So in the view.
‫Let's see.
‫Let's go to views and then our error view.
‫And let's just do like have an A tag and let's have it go to slash.
‫I guess we'll have to go to slash listings.
‫And we'll say go back to.
‫Go back to listings.
‫Yeah, let's center it.
‫So we'll just add class.
‫Text dash center.
‫Uh, actually, no, that's not going to work.
‫We need to make that.
‫Block.
‫There we go.
‫All right, so go back to listings.
‫Um.
‫Why did that go?
‫Why is this being shown here, though?
‫That's.
‫Okay.
‫So the.
‫Let me just check out the view for listings index.
‫You know what?
‫I think I'm loading the home view.
‫So in the listings index.
‫Yeah.
‫Where is it?
‫Right here.
‫So listings index.
‫We don't want to be loading the home view.
‫This should be listings slash index.
‫Wondering why that search was there.
‫All right, so now we're all set.
‫Except that we do want to change the link in both the home view and the listings index to not use that
‫query string.
‫So in views let's go to let's see.
‫We'll go to Home View.
‫And right here instead of question mark ID.
‫ET cetera.
‫What are you going to do listing slash.
‫And then the ID also in the index view.
‫So listings index.
‫Down here.
‫We're going to do listing slash.
‫And then the ID.
‫So let's reload that click details okay.
‫That works.
‫If I go to the home page and I click details on one of these, that also works.
‫All right.
‫Cool.
‫So I would say at least in my opinion that's probably the toughest part of the course, is just that
‫whole infrastructure change.
‫So this will be the end of this section.
‫I'll do a little wrap up in the next video, because there are just some things that I want to say,
‫and then we'll move on to more database stuff updating the listings, deleting them, and then ultimately
‫move on to authentication and authorization.