Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e24 – URL Helpers

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

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!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue