online software programming courses

How to use slugs in laravel?

In this tutorial, you will learn the below things about slugs

  1. What is a slug?
  2. How to create a route for slug in web.php file in Laravel?
  3. How to pull database row related to slug and display in the browser URL bar?
  4. Things to remember when you create slugs in Laravel.

seo-friendly-slug-for-search-engines

1. What is a slug?

Slug is SEO friendly URL link that makes sense to search engines. For example, if you have a URL name like the below one

https://www.dbestech.com/11

This URL would not make sense to any human, let alone to a search engine like Google. Instead of that if you have a URL like the below one

https://www.dbestech.com/10-reasons-to-learn-software-development

This would make more sense to a human as well as a search engine. This kind of human and machine-readable URL is called slugs. Now let's see how to create a slug route.

2. How to create a route for slug in web.php file in Laravel?

In Laravel, in your web.php file create a route like below

Route::get('tutorials/{slug}', 'BlogsController@view_course');

Well, in general, these are all GET requests. If you have lots of different blogs or articles posts, you must have a word or keyword before each slug(I mean between the curly braces in the route). If you don't have a keyword, Laravel would not be able to differentiate which route to call for your certain get request. Of course, you can choose to store all the slugs just in one column(which would be a little messy). In my case, I have used "tutorials" as a keyword. You can use anything you want. This is the simplest way to create a slug route in Laravel. Of course, you can create restrictions in your slugs using regular expressions. 

3. How to pull database row related to slug and display in the browser URL bar?

Say, in your blade or HTML file, you will have a href link like "How to use slugs".

<a href="how-to-use-slugs-in-laravel">How to use slugs</a>

In this href link "how-to-use-slugs-in-laravel" is your slug. Once you click on this kind of href link you will call a function in your controller. In my case, I am calling a function called view_tutorial. In your case, it would be anything you like. See my controller function below

public function view_tutorial($slug){
      
        $pages = Task::where('slug', $slug)->first();
     
        return view('courses.online_tutorial', compact('pages'));
}

In this case, the second line in my function would find a row, where a column name "slug" would match my $slug variable. That's how you find the slug matching row in the database table and pass the information to the blade using return view. 

Remember in your database table, there should be a column, where $slug variable matches one of the values from the column. In general, it's not the ID column.

4. Things to remember when you create slugs

You need to create SEO friendly slugs. To create a friendly URL you must remember the below things when you create one.

  1. Don't use strange characters in slugs
  2. Don't use date in slugs
  3. You can use only lowercase letters
  4. Use a hyphen(-) between each word instead of underscore(_)
  5. It's better don't use underscore 
  6. Try to keep slug length between 50-60 characters long
  7. Don't use more than 5 keywords in your slug so that users remember them
  8. You need to replace the stop words (like a, an, the) using hyphens. Like if you want to name your slug what-is-the-ideal-length-of-an-url-slug, you may replace the(the, a) using hyphens. Now it would become what-is-ideal-length-of-slug.
  9. You should also dynamically create meta tags descriptions for each post uniquely.

Now here is the extra bonus of this tutorial. You can use the below code for a safe SEO friendly URL. Just pass your slug through it. And it will dynamically generate a slug which google likes to index and makes it user friendly and optimized for google crawl. 

private function seo_friendly_url($string){
        $string = str_replace(array('[\', \']'), '', $string);
        $string = preg_replace('/\[.*\]/U', '', $string);
        $string = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '-', $string);
        $string = htmlentities($string, ENT_COMPAT, 'utf-8');
        $string = preg_replace('/&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);/i', '\\1', $string );
        $string = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '-', $string);
        return strtolower(trim($string, '-'));
}

Once this function returns, save it to your database in your slug column.