Laravel 5.8 Tutorial From Scratch - e50 - SEO Friendly URLs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're gonna be talking about SEO friendly URLs now if you're not familiar with what I'm talking about I'm talking about the fact that sometimes in our project we just simply have this slash 1/2 something like that with an ID and really those are not great for SEO purposes Google and other search engines they really want you to have a nice URL so they can identify the content in your website now one example of this that I could give you is in the coders tape website for example up here if you look at the URL we have a lesson slash than an ID but then we do have introduction to PHP unit setup part one so that way Google kind of knows what's inside this page just by simply having a nice SEO friendly URL so today I have a fresh project and I want to show you a couple of different examples on how to make SEO friendly URLs with level so let's get right to it here is my fresh project like I said brand new project I haven't done anything at all the first example that I want to show you is if for example we had this concept of a profile and instead of having profiles slash and then the profile ID I want to have the user name so going with that let's go ahead and create a model PHP artisan make model for a profile and I do need a migration for that all right back to phpstorm let's check out the migration for create profile and we're gonna keep it extremely simple we're just gonna have a string for username and it is gonna be unique and now this is one thing that's important about this technique that I'm showing you this very first technique requires that it be a unique username for example something like an email that could be unique but if it's something that's provided by the user and you're gonna have several examples of it it just simply would not work I will show you how to do those as a second example so we'll have a username and just for the sake of it let's have a name for our user that way we do know that we are retrieving the correct user great so now let's go ahead and create a controller profiles controller all right so let's go back to phpstorm and check out profiles controller in here I will just have a show as that's what I'm interested in and in my show view of course I will have just for now I will go ahead and say profile I won't use route model binding just yet and let's just simply return profile that's all I want to do again this is a little bit of setup so we can get this example going here and let's go ahead and add a route for profile slash and then we know we're gonna have the ID of the profile all simple stuff all stuff that we've covered before encoder stick so profiles controller at show so now in the browser we can just visit slash profile slash one so obviously we don't have any profiles just yet so let's go ahead and migrate our database migrated and now that we are migrated we can run tinker PHP artisan tinker and let's create a new profile so let's say profile new profile let's set our username equal to maybe just user name and then let's set the name equal to Victor I'll just keep it simple like that and so let's go ahead and save that to the database and there we go all right so now if we go back to our browser if I visit profile one then sure enough I get one right whatever I am passing through here that is just simply what is passing through here okay so now that we have all of that set up let's go back here so of course we know that this is gonna be an ID in typical fashion typically you would say something like profile is gonna equal to profile right and then we're going to import that up here at the top and then we'll go ahead and find the profile with that ID and then I can just return profile does that work yes it does so we have our record back so if we wanted to say profile slash username right pertaining to this username right here then of course we could say profile where username is equal to profile let's go ahead and fetch the first record of that and if we visit profile slash username sure enough we still get the same thing but what about route model binding isn't it awesome that we could just say well this is a profile laravel you go ahead and handle everything for us right this is kind of what we want to do but if we try that as username of course that doesn't work we are stuck with that slash one so here's how you make that work if we jump into my profile model and we actually go into the parent model which is this one and we do a search here for a get route key name and there it is so we can override this get route key name and set up whatever we actually want the column to be so check this out it's actually quite simple in my profile I'm gonna go ahead and overwrite that get route key name and all I need to do here is return a string with the column that I want to use in my case I want to use username so if we do that we can actually revert back to using our username and there it is so how cool is that so that's how we make route model binding do we needed to do and we get to set what we want to do but this breaks down fairly quickly because this only really works if you have a unique constraint on that database but sometimes if you're doing for example blog posts a title may be used more than once there's no reason why a user could not name two blog posts exactly the same title so why don't we look at an example for that let's go ahead and exit out of tinker here and let's create a new model and this one is going to be for posts and I do need a migration for that and while we are at it let's go ahead and make a controller because I do know I'm gonna need one of those so my post controller alright let's head back to phpstorm and take a look at that example let's go to the migration create post table and let's keep this very simple and just have a title that's all I want to have so in my post controller I will also just have a show and we know we're gonna have route model bindings so why don't we say post posts for that that way we are taking advantage of route model binding we'll just return the post just to make sure that we did fetch the correct thing from our database so I need a new route and it's going to be for posts and it's gonna have a post and this is going to be in my posts controller okay so let's head back here and run PHP artisan migrate and now we have our post table so let's go ahead and create one now PHP artisan tinker and let's create a new post and all we have for this post is just a title so I will just say my cool title and then let's go ahead and save that to the database there we go alright so we are good to go on that let's head back to chrome and I will go to slash post slash one and if we go in here it looks like we didn't import that properly let's go back here to my post controller yep alright let's go ahead an import post up here at the top there we go so that will work and refresh and there we go alright so we are properly fetching that from our database so my cool title the nice thing with this title again is that it could be used more than once so we can't just simply rely on something like my cool title because if we did then we would not be able to have repeated posts so one trick that I found to be very useful is to have the ID and then a dash and then a Slugger fide version of the title my cool title how about that now the nice thing with this is that you actually don't have to do a lot of work to make it work check this out if we go back to phpstorm we go into our web browser file we're gonna have post slash then our post meaning the ID then let's tell level we're actually gonna have a - and after the - I'm gonna have a slug and that's it as long as we have a slug right afterwards it doesn't matter what we do this so one - my cool title in slug form and if we check that out yep it still works to show you the difference if I remove that all together and I hit refresh now we get a 404 not found so even though we still have that one it's not able to match to my route now if I add the dash and then a slug for a variable and we hit refresh yep and we really don't care about the slug I mean you could theoretically short pull in the slug if you wanted to in your post controller right here we could pull that in as slug but we really don't even need it this is really meant for just SEO purposes again the nice thing with this is that if we had another post let's go ahead and add another post with the same exact title so now we have two posts right and I'll show you that by just calling post all we have two posts in our database and both of them have the exact same title but they won't conflict check it out if we hit refresh here yep that still works and if we had a two for the second ID that also still works so this is nice because it actually eliminates the need to have something funky like one and then - two - three you don't need to do that obviously your ID is always going to be unique so that's nice we're taking advantage of that unique ID to have a nice SEO friendly URL all right one more trick that I want to show you and it is the following of course once you get into creating these more complex routes you don't want to have to type this in every single time that you need that route so what we can do is I will actually pull up the Welcome that blade this is just what ships with Lenovo and somewhere down here of course it says levo let me go ahead and delete everything off of this and all I'm gonna do here is I'm going to add a link and I just want to link to slash post / 1 - my cool title very complex right I'd really don't want to be typing this up every single time so I'll just name this link so we have this set up here let's go to my post controller and instead of returning the post let's go ahead and return a view of welcome and with that being set let's go ahead and refresh this and of course if we hit link we're basically pointing to itself and that's okay we see that that link is working so one cool thing that you can do is the following let's go ahead and pass that post inside our view and then in my post model what I can do is a nice little helper to help me know exactly what the route is for that post so let's go to post that PHP and you can call this whatever you want typically I just call it path you can call it URL that's the nation that you can name this whatever you'd like let's just call it path and in here we can return the actual URL that we're going to use now if you want a fully qualified URL you can actually use the URL helper and then inside of here we know we're gonna have slash post slash and then the post ID so we could say this ID remember we are inside a post at this point and then we're going to have a slash and then we're going to have this log applied title so we can use s2 you are that's illuminate support STR notice it's being imported up here at the top and then we can call slug and on that we can pass this title so this is a nice little helper now every time I need to know what the path is to this post all I need to do let's go back to my welcome is call my post and that will give me the path to this post every single time hit refresh and if we look click on it yep it still works how cool is that so now we have a nice way if tomorrow for some odd reason I decided to change the way that I actually see that then all I need to do is go ahead and just change it in this one place right here and we got brand new URLs across the entire site I never hard-coded an actual URL into my view I'm simply just calling this path again you can call this whatever you'd like and you could get the fully qualified URL to this particular resource I hope that helps and gives you a nice visual on the two most common ways that I use to create SEO friendly URLs as always if you have any questions drop them in the comments below don't forget to subscribe my name is Victor thanks for watching
Info
Channel: Coder's Tape
Views: 18,727
Rating: undefined out of 5
Keywords: phpunit, laravel phpunit testing, phpunit laravel, laravel testing controllers, laravel feature testing, laravel unit testing controllers, unit testing laravel, laravel 5.8, laravel testing, laravel testing tutorial, laravel phpunit, laravel test workflow, seo optimization, laravel seo tutorial, seo laravel, laravel url slug, url slug php, url slug laravel, slugify, seo friendly content writing, seo friendly website, seo friendly, google optimize
Id: LmTcMwurJsA
Channel Id: undefined
Length: 13min 44sec (824 seconds)
Published: Mon May 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.