Blog Detail

10

Aug
Easily Create Shortened URLs for Your Web Apps in Laravel cover image

arrow_back Easily Create Shortened URLs for Your Web Apps in Laravel

Short Url is a fantastic Laravel package that can be used for adding shortened URLs to your existing web app.

Installation

You can install the package via Composer:

composer require ashallendesign/short-url

Next, publish the package’s config file and database migrations by the following command:

php artisan vendor:publish --provider="AshAllenDesign\ShortURL\Providers\ShortURLProvider"

To run migrations, simply run the following command:

php artisan migrate

Building Shortened URLs

Quick Start

The quickest way to create a shortened URL is by using the snippet below. The ->make() method returns a ShortURL model from which you can grab the shortened URL.

$builder = new \AshAllenDesign\ShortURL\Classes\Builder();

$shortURLObject = $builder->destinationUrl('https://destination.com')->make();
$shortURL = $shortURLObject->default_short_url;

Custom Keys

By default, the shortened URL that is generated will contain a random key. The key will be of the length that you define in the config files (defaults to 5 characters). For example: if a URL is https://webapp.com/short/abc123, the key is abc123.

You may wish to define a custom key yourself for that URL that is more meaningful than a randomly generated one. You can do this by using the ->urlKey() method. Example:

$builder = new \AshAllenDesign\ShortURL\Classes\Builder();

$shortURLObject = $builder->destinationUrl('https://destination.com')->urlKey('custom-key')->make();
$shortURL = $shortURLObject->default_short_url;

// Short URL: https://webapp.com/short/custom-key

Tracking Visitors

You may want to track some data about the visitors that have used the shortened URL. This can be useful for analytics. By default, tracking is enabled and all of the available tracking fields are also enabled. You can toggle the default options for the different parts of the tracking in the config file. Read further on in the Customization section to see how to customize the default tracking behaviors.

Enabling Tracking

If you want to override whether tracking is enabled or not when creating a shortened URL, you can use the ->trackVisits() method. This method accepts a boolean but defaults to true if a parameter is not passed.

The example below shows how to enable tracking for the URL and override the config variable:

$builder = new \AshAllenDesign\ShortURL\Classes\Builder();

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->make();

Tracking IP Address

If you want to override whether IP address tracking is enabled or not when creating a shortened URL, you can use the ->trackIPAddress() method. This method accepts a boolean but defaults to true if a parameter is not passed.

The example below shows how to enable IP address tracking for the URL and override the default config variable:

$builder = new \AshAllenDesign\ShortURL\Classes\Builder();

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->trackIPAddress()->make();

Enforce HTTPS

When building a shortened URL, you might want to enforce that the visitor is redirected to the HTTPS version of the destination URL. This can be particularly useful if you’re allowing your web app users to create their own shortened URLs.

To enforce HTTPS, you can use the ->secure() method when building the shortened URL.

The example below shows how to create a secure shortened URL:

$builder = new \AshAllenDesign\ShortURL\Classes\Builder();

$shortURLObject = $builder->destinationUrl('http://destination.com')->secure()->make();

// Destination URL: https://destination.com

This package offers many handy features and methods with code examples. If you want to learn more about this package then please visit Github.

Published at : 10-08-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project