switchon@lavalamp.biz
+27(0) 83 419 4851 / +27(0) 21 036 1165
Application & software development
Get A Quote

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact
    ubersuggest seo analyzer
    SEO Tips I Recently Learnt On Ubersuggest
    Jul 19, 2021
    distributed ledger technology
    What Is Distributed Ledger Technology?
    Aug 12, 2021

    Simple URL Shortener For Your Encoded URLs In PHP Laravel

    simple url shortener
    power bi

    A 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');
    

    Contact us


      Related posts:

      laravel livewireLaravel Livewire dynamic doughnut graph using laravel vue componentDynamic Doughnut Graph Using Laravel Vue Component laravel observersLaravel Observers how to deploy your app/website on herokuHow To Deploy Your App/Website On Heroku
      Share
      43
      Abduraghmaan Gabriels
      Abduraghmaan Gabriels
      Abduraghmaan is a young up-and-coming full-stack software engineer. He started his journey in the world of tech by modifying the small scripts in old Pokemon games and ended up falling in love with coding. His ultimate passion is bringing new people into the world of technology and teaching them the wonders of Software development.

      Leave a Reply Cancel reply

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

      Lava Lamp Lab


      Like technology, a lava lamp constantly changes form, producing new conditions with every passing moment



      lava lamp lab facebook   lava lamp lab twitter   lava lamp lab linkedin   lava lamp lab instgram

      Services


      Application & software development

      Outsourced software development

      Project based resourcing

      Digital marketing & consulting

      Graphic design & consulting

      UI / UX design & consulting

      Contact Us


      +27(0) 83 419 4851

      +27(0) 21 036 1165


      switchon@lavalamp.biz


      Unit 1 Monaco Square,
      14 Church Street,
      Durbanville,
      Cape Town, 7550

      NBConsult Group


      nbconsult
      nbconnect msp
      nbclearning
      river broadband
      designer needed
      © 2023 Lava Lamp Lab (Pty) Ltd | All Rights Reserved | Privacy Policy
      Contact us now

        Application & software development

          Outsourced software development

            Project based resourcing

              Digital marketing & consulting

                Graphic design & consulting

                  UI/UX design & consulting

                    Lease an expert

                      Recruitment services

                        We are using cookies to give you the best experience on our website.

                        You can find out more about which cookies we are using or switch them off in settings.

                        Lava Lamp Lab
                        Powered by  GDPR Cookie Compliance
                        Privacy Overview

                        This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

                        Strictly Necessary Cookies

                        Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

                        If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

                        3rd Party Cookies

                        This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

                        Keeping this cookie enabled helps us to improve our website.

                        Please enable Strictly Necessary Cookies first so that we can save your preferences!

                        Cookie Policy

                        More information about our Cookie Policy