Generate SEO friendly URL in Laravel

Generate SEO friendly URL in Laravel


To rank your website in search engine and getting organic traffic to your Laravel made website, it's very much important to generate your laravel made website links in SEO friendly formation. Most of the developers are not concern about making proper and SEO friendly URL formation so that their website not rank to search engine. Some of the developers generate website URL like as backend application something like (example.com/123 or example.com/posts/123). Website URLs are shown in SERP ( Search Engine Result Page) along with the webpage title. If you do not use the SEO friendly URL side by side with webpage title then the URL does not represent what is the content of that URL and it will not help your website to match the user search keyword. As a result, your website does not get rank and you lose more website traffic from the search engine like Google, Yahoo, Bing etc. Here in this post, I'll show you step by step guide for generating SEO friendly URL for your Laravel made website.

 

SEO friendly URL formation

For any blog/article related website we can generate URL for a single post, blogs/articles page, category page like below.

DO

URL format Route
example.com/posts Route::get('/posts','SiteController@posts');
example.com/categories/tips-and-tricks Route::get('/categories/{slug}','SiteController@categoryPosts');
example.com/posts/awesome-post-slug Route::get('/posts/{slug}','SiteController@postDetails');

 

Don't

example.com/posts/123

example.com/posts?id=123

example.com/categories/45

example.com/category?id=45

 

SEO Friendly URL generation process

  • Add column slug to tables.
  • Model setup for unique slug and link.
  • Site Controller code.
  • View code and link generation.

 

Add slug column to tables

We'll generate URLs with by unique slug for more readable and SEO friendly instead of using record id. To make sluggable URL we need to add a unique type slug column into our categories and posts table if it's not added before.

 

Model setup for unique slug and link

We need cviebrock/eloquent-sluggable package for unique slug URL generation. Let install that package.

composer require cviebrock/eloquent-sluggable

 

Category model

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;

class Category extends Model
{
    use Sluggable;

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'name'
            ]
        ];
    }

    public function getLink()
    {
        return url('categories/'.$this->slug);
    }

    public function posts()
    {
        return $this->hasMany('App\Post');
    }

    //other code

}

This category model setup will automatically generate unique slug URL for us from category name during category create and the getLink will help us to get our desire link from every category object.

$category = new Category([
    'name' => 'Tips and Tricks',
]);

$category ->save();
//$category->slug is "tips-and-tricks"

 

Post model

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Sluggable;

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }

    public function getLink()
    {
        return url('posts/'.$this->slug);
    }

    public function category()
    {
        return $this->belongsTo('App\Category');
    }

    //other code

}

This post model setup will automatically generate a unique slug URL for us from post title during post create and the getLink will help us to get our desire post link from every post object.

$post= new Post([
    'title' => 'Awesome Post Title',
]);

$post->save();
//$post->slug is "awesome-post-title"

 

Site Controller Code

Let's do the code for our site controller for view response according to our route definition.

<?php

namespace App\Http\Controllers\Site;

use App\Category;
use App\Post;


class SiteController extends Controller
{

	public function posts(){
		$data = Post::with('category')->paginate(20);
		return view('site.posts',compact('data'));
	}

	public function categoryPosts($slug){
		$data = Category::with('posts')->where('slug',$slug)->first();
		return view('site.category-posts',compact('data'));
	}

	public function postDetails($slug){
		$data = Category::with('category')->where('slug',$slug)->first();
		return view('site.post-details',compact('data'));
	}

}

 

View code and link generation

Getting all post with pagination in example.com/posts

<!--resources/views/site/posts-->

<h2>Posts</h2>
@foreach($data as $post)
	<div>
		<p><a href="{{$post->getLink()}}">{{$post->title}}</a></p>
		<p>Category: <a href="{{$post->category->getLink()}}">{{$post->category->name}}</p>
	<div>
@endforeach

Look in view we don't need to manually generate our post link, category link. By the getLink method, it'll automatically do for us according to our model getLink method definition.

 

Get category wise posts in example.com/categories/my-category

<!--resources/views/site/category-posts-->

<h2>Category: {{$data->name}}</h2>
@foreach($data->posts as $post)
	<div>
		<p><a href="{{$post->getLink()}}">{{$post->title}}</a></p>
		<p>Category: <a href="{{$post->category->getLink()}}">{{$post->category->name}}</p>
	<div>
@endforeach

 

Post details in example.com/my-unique-post-title

<!--resources/views/site/post-details-->

<h2>{{$data->name}}</h2>
<p>Category: <a href="{{$data->category->getLink()}}">{{$data->category->name}}</p>

<p>{!!$data->description!!}</p> 

 

In above I have shown you step by step process for making SEO friendly URL in Laravel Framework for a blog/article website. According to this process, you can generate SEO friendly URL in Laravel framework for other kinds of website like E-commerce, directory listing site, news site etc. Hope you enjoyed this post. If you think this step by step tutorial post help you then share this post with others so that they can be helped for learning how to generate SEO friendly URL in Laravel. Thank you for reading this entire post with patience.

 

Share on


sidebar ad


Related Post


Laravel Barcode generation tutorial

Ultimate Laravel SEO guide in 2020

Dynamic SEO meta-tags in Laravel website

Generate dynamic sitemap in Laravel - SEO