For the best experience, follow along in our interactive school at
Search Engines prefer that your application have nice URLs instead of just an ID of a resource. In this lesson, we are exploring 2 ways to achieve this in Laravel.
DigitalOcean Referral
Resources
Course Source Code
Hit us up on Twitter with any questions or comments @codertape ()
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!
Hello,
Thanks for this video about SEO friendly URLs, i’ve a question not according to this topic but about live search, just to illustrate, for instance, i’ve a register form and username must be unique, l’d like to inform user when he insert his username in input field that username is already used or it’s free, how to do that simple as possible.
Thanks
Hahaha you’re welcome
+Coder’s Tape Thank you for your rapid reply, master wizard of laravel 😉
The way that would work is by detecting onblur on that input field. When that event happens (basically the user has left the field) then you send an AJAX request back to the server. It should return wether the username is taken or not and then you will update your UI accordingly.
Another way is you can detect when the user stops typing, something like after 3 seconds of not typing in the field, hit the server to check it.
Thank you so much for this series.
Thank you for the video sir.
Just a question on how can I access validation error if my create and edit modal is on the same page?
+Coder’s Tape Thank you sir.
Did you check out the videos on RESTful controllers in this series? I think it might answer your questions.
Always great contents 😉
But what if we use uuid? The url becomes too long before the slug.
Do you have advices on this?
+Coder’s Tape , this is pure gold!
Thanks 😉
Couple of things to consider…
1. /posts/my-cool-title/HUGEUUIDHEREATTHEND
Your route would look something like Route::get(‘/posts/{slug}/{uuid}, ‘PostsController@show’);
2. /posts/my-cool-title?uuid=HUGEUUIDHEREATTHEND
Your controller can validate for uuid field and require it (if necessary) Search Engines don’t look at query strings in the URL so that would be a nice workaround.
All and all, if you have to use UUIDs to ensure uniqueness there’s no way of getting away from a huge URL.
Hope that helps.
Thanks again for the great video! Could you explain what are the pros and cons (if any) of generating slugs on the fly like you did versus storing them in a database?
+Coder’s Tape Interesting, thanks! I will definitely try it in my next project.
Great point! Generating them on the fly is no better or worse in terms of performance. The server can slugify a string in a split second BUT the one thing that does happen is that if the title of your post changes then your slug changes as well. You can have no control over keeping the original slug for that post. However, the best news is that even if the title/slug changes, the url still works with Laravel. Reason being that we are actually using the ID to fetch the record and that never changes. Give it a try and you’ll see.