SEO Tips I Recently Learnt On Ubersuggest
July 19, 2021What Is Distributed Ledger Technology?
August 12, 2021A problem almost every web or full stack developer will eventually come across.
This is a simple step-by-step guide to creating your own very own URL shortener.
The core concept is very much universal across any web development stack, whether you would like to set it up as a standalone service, or just implement it in your projects.
First thing that we need to establish is the database table that will be used ( we’ll call ours “mini_urls” for now ). In General you want to have the first 2 fields regardless of the rest of the data.
base_path - which will hold the original URL
short_path - which will store the shortened version of the URL
external_domain - this is an optional field for any redirecting to external URLs
We can start by creating a model for the database table which should look similar to the following:
namespace App; use Illuminate\Database\Eloquent\Model; class MiniURL extends Model { protect $table = 'mini_urls'; protected $fillable = ['base_path' , 'short_path', 'exteneral_doamin']; }
Create a folder in your app/http directory called traits and then create a file called MiniURLTrait.php.
This file should have the following code:
namespace App\Http\Traits; use App\MiniURL; trait MiniURLTrait { function secure_random_string($length) { $random_string='' ; for ($i=0; $1 < $length; $i++) { $number=random_int(0, 36); $character=base_convert($number, 10, 36); $random_string .=$character; } return $random_string; } function generateShortURLPath($base_path,$external_domain=null) { $short_path=$this->secure_random_string(8); $mini_url = MiniURL::firstOrCreate( [ "base_path" => $base_path, "external_domain" => $external_domain, ], [ "short_path" => $short_path ] ); return $mini_url; ) }
What the secure_random_string function does is create a random cipher-protected alphanumeric string of any length.
The generateShortURLPath function takes the original URL path and pairs it with the random string we generated and saves it in our table using the MiniURL model we created earlier.
We then have to create a controller called URLController with the following command in your terminal:
php artisan make:controller URLController
In this file we first want to add the following lines above the class definition and the 2 functions within the class:
use App\MiniURL; use Illuminate\Http\Request; use App\Http\Traits\MiniURLTrait;
funcation shotenURL(Request $request) { $base_path=$request->base_path; if(isset($request->base_domain)) { $base_domain = $request->base_domain; } else { $base_doamin = env('APP_URL); } $link = base64__encode($base_path); $short_url = $this->generateShortURLPath($link,$base_domain); return ($base-domain . '/short/' . $short_url->short_path); } funcation getURLFromMiniURL(Request $request) { $short_path = $request->short_path; $mini_url = MiniURL::where("short_path",$short_path)->first(); if($mini-url) { $base_path = base64_decode($mini_url->base_path); if($mini_url->external_doamin) { return redirect() ->away($mini_url->external_doamin . base_path); } return redirect($mini_url->base_path); } else { return response()->json(["error"=>'url not registered']); } }
The '/short/' text we are appending to the URL is the endpoint we will be using when redirecting to the original URL.
Lastly we can test these functions by creating our endpoints in the relevant Laravel router file:
Route::get('/short/{short_path}' , 'URLController@getURLFromMiniURL') ->name('get-url-mini'); Route::post('/shorten-url' , 'URLController@shortenURL') ->name('shotern-url');