Laravel 5.8 – From The Ground Up
Lesson 24 of 52 · Laravel 5.8 Tutorial From Scratch – e24 – URL Helpers · Education
Laravel 5.8 Tutorial From Scratch – e24 – URL Helpers
up next in our learning we're gonna be talking about URLs and there are three helpers in laravel that help us generate URLs for our app now there are several
reasons to use this as opposed to what we've been doing so far but let's start off just by reviewing what it is that you may be used to doing let me visit the create method inside our contact
page what I'm talking about is something like this obviously we have an action and we are headed to slash contact and we've done this throughout the project and typically this is just how you
handle URLs in your app however sometimes you have issues with addresses like this because they are relative and so what happens is that it is actually completely coupled to that exact URL if
we went to the route file and for some odd reason decided to change contact and maybe contact us it all breaks down we would have to find every instance where we use that and change it even though we
like the idea of having this route file because it gives you the one place where everything about your routes is stored and that is very powerful but we're not currently using it right now so let's
start using some of it the first helper that I want to show you is a simple URL creator so I'm gonna actually cut that out and let's put our curly brackets which is basically just
our blade syntax for echoing out and let's use the URL helper and we'll pass it in the exact same string to the URL helper now let's head back to chrome and visit the contact us page and here's
what I want to show you I popped open the source code and if we scroll down a little bit to the part that we're looking at we see here that we have a nice fully qualified path to our
endpoint and let me show you the difference I'm gonna undo this a couple of times and just go back to this and hit refresh and so you see the difference here so we have a relative
path if tomorrow any of that changed we don't have a direct path so your URL path actually gives you a really nice full path to any URL that you pass in like so in our case right now that is
just matching to our testing environment using PHP artisan surf which have been using the whole time but once you have your website actually up in your production server that will give you a
nice fully qualified path to your resource or endpoint including your domain name however that still has the same problem that if we decide to change contact well we'd have to go back and
change contact in every instance so how do we fix that and the simple answer to that is something called named routes you can assign a name to all of your routes and it's very simple all you have
to do is add a name and pass in a string so by convention I'm gonna use contact dot we'll use the dot notation create and then let's do a named route on our store method as well so we'll say name
contact dot store ok let's save that so how do we use that instead of the URL helper let's use the route helper and in the route helper we'll say contact dot store hit save let's go back to Chrome
hit refresh you get the exact same thing right here but the difference here is in our web routes if I decide to change this from contact to contact us for example and of course we'll have to
change the address up here now contact us there we go let's scroll back and there you are our address has been updated in our view even though we didn't explicitly change anything in the
view as you can tell right here all we're doing is using the route helper and using the name of our route instead of using the explicit route to our file now one thing is we're using this
resource and when you use a resource you can't call the name on it but through the magic of laravel it actually names all of your routes for you it's pretty awesome I'm going to show you a new
command that we haven't talked about yet and that is route list let me show you the command first in PHP artisan and it's right here route list so route list will list all of the routes for your
application so let's run that now PHP artisan route list and there we are so these are all of the routes that have been assigned to our application let me make this slightly smaller there we go
think it's a little bit easier to see so we have our customers resource down here you see all seven verbs are here and they're all properly named laravel does that for us already so you have
customers dot index that store that create update and so on and so forth and we do see the two names that we added to our contact routes and those are right here and of course we haven't named our
home route doesn't have a name the about route doesn't have a name but do notice that the login and all of the auth scaffolding that we added with the PHP artisan make off have named routes as
well so if you want to redirect to a logout you actually just pass the string logout to our route and levo will properly route that for us so that's pretty awesome
the other thing i want to show you is that you can actually call a method on a controller directly from a view say that I wanted this route right here slash home let's see what that looks like in
the browser let's go to slash home let me login test that test com so now that we're logged in we go to slash home you see that we get this dashboard you are logged in this is one
of those scaffolded pages that letter though gave us now this happens to have a named route but I will remove the name route altogether and what I want to target is actually this right here so
let's go to my navigation and just for now I'm gonna add another one and I'm just gonna call it dashboard and what is the URL gonna be so we have another helper and the helper is action so if we
called action we can pass the exact same string that we're passing to our route that is home controller at index let's check what that looks like paste alright let's go back to the
browser hit refresh and if we inspect dashboard we have a fully qualified path to that controller's URL even though we are not referencing the route at all we're actually targeting our controller
and the specific method inside the controller pretty cool and definitely very useful all right let's get rid of this let's go back I want to show you a different notation of this if you are
using an IDE like I am like phpstorm you can typically hold command and kind of click your way into a definition or a declaration of a class and in this current notation that wouldn't work
however let me show you the alternate way that you can call that that will actually allow you to use the intelligence in your IDE so instead of passing a string what we need to do is
pass it an array and the first value that we need to pass in is the full name space to our controller so let's do that now that would be app HTTP controllers and they were looking for the home
controller class and as a second parameter we can pass in index and if we hit safe and come back hit refresh you see it still works we're still getting the URL to our controller now the
difference with this notation is that now I can actually command click through my ID so my IDE is able to parse that unlike passing in a string when you pass in a string it's not able to do that I'm
gonna undo go back to the string and again if I try to command click it just simply says nope I can't find a declaration to go to and that makes sense because that's just a string so
that would be using the action now I don't particularly love using action to be honest with you but it's always good to know about it because you might see it in a project I definitely prefer to
use a name route like this or simply use the URL helper for all of my routes alright just to wrap everything up I need to show you one more thing before we can refactor our entire project I
need to go to the customers edit method and there's this right here so this action requires a parameter so we haven't really touched up on how to do that just yet so let me show you how to
do it we can continue to use our route and we'll pass in customers dot update right because remember this is the edit view which means we're sending a patch request to customers to be able to
update the details in our resource so customers dot update and then as a second parameter we're gonna pass in an array and inside the array customer and I'll show you why in just a second and
we'll match up customer to customer and letter bill is actually smart enough to know that customer is an eloquent model and we'll grab the appropriate ID for us so that's pretty cool
now let me show you something this customer if we look through here you see that it says customers and then customer so it will grab this string here customer and that is what we're matching
to right over here so now we're able to use the route method in a route that requires some additional parameters you of course can pass in as many parameters as you need here we'll say another one
it doesn't matter how many you have you can pass as many as you need inside that array so in our case we just have the one that is just customer and that's no problem go ahead and find every instance
that we've used a URL in and convert it to either a named route or using the URL helper and I will do the same and when I'm done I'll go ahead and upload the changes to get help
In this episode, we explore the 3 URL Helpers offered by Laravel out of the box and refactor our project to use them.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!