How to create url shortener using Laravel?

How to create url shortener using Laravel?

In this post we will give you information about How to create url shortener using Laravel?. Hear we will give you detail about How to create url shortener using Laravel?And how to use it also give you demo for it if it is necessary.

Today, i would like to share you how to generate short url in laravel application. sometime we need to make shorten url string instead of long url. in this tutorial i will show you step by step how to create shorten a url in laravel 5 without any package. you can write your own script function for generator short link in laravel.

If you are working with SMS or some where share with limited character and at that time if you need to share any url or link then you must have to generate shortener url. In your project it’s need many times then you must have to implement module that can help to generate url shortener with your website domain.

You need to just follow step by step this tutorial, you will get very simple module for generate short link in laravel 5. i will also give you free download whole script.

Preview:

Step 1: Install Laravel 5

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

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

Step 2: Create Table

we are going to create from scratch application for create short link. so we have to create migration for “short_links” table using Laravel 5.8 php artisan command, so first fire bellow command:

php artisan make:migration create_short_links_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 short_links table.

<?php

use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class CreateShortLinksTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->bigIncrements('id');

$table->string('code');

$table->string('link');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('short_links');

}

}

Now you have to run this migration by following command:

php artisan migrate

Also see:How to Create Widgets in Laravel?

Step 3: Create Model

In this step, now we should create new model as ShortLink. So run bellow command and create new model. run bellow command:

php artisan make:model ShortLink

app/ShortLink.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class ShortLink extends Model

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'code', 'link'

];

}


Step 4: Create Route

In this is step we need to create route for layout file and another one for store data. so open your routes/web.php file and add following route.

routes/web.php

Route::get('generate-shorten-link', 'ShortLinkController@index');

Route::post('generate-shorten-link', 'ShortLinkController@store')->name('generate.shorten.link.post');

Route::get('{code}', 'ShortLinkController@shortenLink')->name('shorten.link');

Step 5: Create Controller

In this point, now we should create new controller as ShortLinkController. this controller will manage layout and store data to database, so put bellow content in controller file:

app/Http/Controllers/ShortLinkController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppShortLink;

class ShortLinkController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index()

{

$shortLinks = ShortLink::latest()->get();

return view('shortenLink', compact('shortLinks'));

}

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

$request->validate([

'link' => 'required|url'

]);

$input['link'] = $request->link;

$input['code'] = str_random(6);

ShortLink::create($input);

return redirect('generate-shorten-link')

->with('success', 'Shorten Link Generated Successfully!');

}

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function shortenLink($code)

{

$find = ShortLink::where('code', $code)->first();

return redirect($find->link);

}

}

Step 6: Create View

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

resources/views/shortenLink.blade.php

<!DOCTYPE html>

<html>

<head>

<title>How to create url shortener using Laravel? - ItSolutionStuff.com</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" />

</head>

<body>

<div >

<h1>How to create url shortener using Laravel? - ItSolutionStuff.com</h1>

<div >

<div >

<form method="POST" action="{{ route('generate.shorten.link.post') }}">

@csrf

<div >

<input type="text" name="link" placeholder="Enter URL" aria-label="Recipient's username" aria-describedby="basic-addon2">

<div >

<button type="submit">Generate Shorten Link</button>

</div>

</div>

</form>

</div>

<div >

@if (Session::has('success'))

<div >

<p>{{ Session::get('success') }}</p>

</div>

@endif

<table >

<thead>

<tr>

<th>ID</th>

<th>Short Link</th>

<th>Link</th>

</tr>

</thead>

<tbody>

@foreach($shortLinks as $row)

<tr>

<td>{{ $row->id }}</td>

<td><a href="{{ route('shorten.link', $row->code) }}" target="_blank">{{ route('shorten.link', $row->code) }}</a></td>

<td>{{ $row->link }}</td>

</tr>

@endforeach

</tbody>

</table>

</div>

</div>

</div>

</body>

</html>

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow url on your browser:

Also see:Laravel 5 – Full Text Search Example using Scout and Algolia Packages

http://localhost:8000/generate-shorten-link

You can download whole code from here: Download Code from Github.

I hope it can help you…

Hope this code and post will helped you for implement How to create url shortener using Laravel?. 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 *

74  +    =  76

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