Laravel 5.3 – How to create SEO friendly sluggable URL

Laravel 5.3 – How to create SEO friendly sluggable URL

In this post we will give you information about Laravel 5.3 – How to create SEO friendly sluggable URL. Hear we will give you detail about Laravel 5.3 – How to create SEO friendly sluggable URLAnd how to use it also give you demo for it if it is necessary.

In this post, I am going to share with you How to generate SEO friendly URL in Laravel 5 application.

SEO is very important part of website for increase users traffic. If your website have seo friendly URL then it can help to increase your site rand in google index, yahoo etc. So If you require to generate SEO friendly URL in your laravel application then We can do it by “eloquent-sluggable” package. eloquent-sluggable package developed by cviebrock.

Eloquent-sluggable package provide to automatic create unique url for our post. this package create unique slug url that way it is good for SEO.

You can do it for your laravel application by following step or if you want to do it from scratch then you can get fresh laravel by following step 1. After finish this example you will get bellow output.

Preview:

Step 1: Install Laravel 5.3 Application

In this step, if you haven’t laravel 5.3 application setup then we have to get fresh laravel 5.3 application. So run bellow command and get clean fresh laravel 5.3 application.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install Package

In this step we have to add eloquent-sluggable package for generate unique slug url so one your cmd or terminal and fire bellow command:

composer require cviebrock/eloquent-sluggable

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php

'providers' => [

....

CviebrockEloquentSluggableServiceProvider::class,

]

.....

You can publish the default configuration file by following command:

php artisan vendor:publish --provider="CviebrockEloquentSluggableServiceProvider"

Also see:How to create custom helper in Laravel?

Step 3: Create Item Table and Model

In this step we have to create migration for items table using Laravel 5.3 php artisan command, so first fire bellow command:

php artisan make:migration create_items_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.

use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateItemsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('items', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->string('slug');

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::drop("items");

}

}

After create “items” table you should create Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:

app/Item.php

<?php


namespace App;


use IlluminateDatabaseEloquentModel;

use CviebrockEloquentSluggableSluggable;


class Item extends Model

{


use Sluggable;


public $fillable = ['title'];


/**

* Return the sluggable configuration array for this model.

*

* @return array

*/

public function sluggable()

{

return [

'slug' => [

'source' => 'title'

]

];

}

}

?>

Step 4: Create Route

In this is step we need to create route for listing items lists and create new item. so open your routes/web.php file and add following route.


routes/web.php

Route::get('items', 'ItemController@index');

Route::post('item-create', ['as'=>'item-create','uses'=>'ItemController@create']);

Step 5: Create Controller

In this point, now we should create new controller as ItemController in this path app/Http/Controllers/ItemController.php. this controller will manage all listing items and create new item request and return response, so put bellow content in controller file:

app/Http/Controllers/ItemController.php

<?php


namespace AppHttpControllers;


use IlluminateHttpRequest;


use AppHttpRequests;

use AppItem;


class ItemController extends Controller

{

/**

* Get the index name for the model.

*

* @return string

*/

public function index()

{

$items = Item::all();

return view('items',compact('items'));

}


/**

* Get the index name for the model.

*

* @return string

*/

public function create(Request $request)

{

$this->validate($request,['title'=>'required']);

$items = Item::create($request->all());

return back();

}

}

?>

Step 6: Create View

In Last step, let’s create items.blade.php(resources/views/items.blade.php) for layout and we will write design code here and put following code:

resources/views/items.blade.php

Also see:How to Generate Slug using str_slug() helper in Laravel?

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.3 - How to create seo friendly sluggable URL</title>

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

</head>

<body>


<div >


<h2>Laravel 5.3 - How to create seo friendly sluggable URL</h2><br/>


<form method="POST" action="{{ route('item-create') }}" autocomplete="off">

@if(count($errors))

<div >

<strong>Whoops!</strong> There were some problems with your input.

<br/>

<ul>

@foreach($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


<input type="hidden" name="_token" value="{{ csrf_token() }}">


<div >

<div >

<div >

<input type="text" id="title" name="title" placeholder="Enter Title" value="{{ old('title') }}">

<span >{{ $errors->first('title') }}</span>

</div>

</div>

<div >

<div >

<button >Create New Item</button>

</div>

</div>

</div>

</form>


<div >

<div >Item management</div>

<div >

<table >

<thead>

<th>Id</th>

<th>Title</th>

<th>URL</th>

<th>Creation Date</th>

<th>Updated Date</th>

</thead>

<tbody>

@if($items->count())

@foreach($items as $key => $item)

<tr>

<td>{{ ++$key }}</td>

<td>{{ $item->title }}</td>

<td><a href="">{{ URL::to('/') . '/item/' . $item->slug }}</a></td>

<td>{{ $item->created_at }}</td>

<td>{{ $item->updated_at }}</td>

</tr>

@endforeach

@else

<tr>

<td colspan="4">There are no data.</td>

</tr>

@endif

</tbody>

</table>

</div>

</div>


</div>


</body>

</html>

You can get more information about package from here : Click Here.

Maybe it can help you.

Hope this code and post will helped you for implement Laravel 5.3 – How to create SEO friendly sluggable URL. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  18  =  22

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US