Blog Detail


Create shortened URLs for your Web Applications in Laravel cover image

arrow_back Create shortened URLs for your Web Applications in Laravel

Ash Allen introduced a Laravel package called Short Url that can be used for adding shortened URLs to your existing web applications.


The package has been developed and tested to work with the following minimum requirements:

  • PHP 8.0
  • Laravel 8.0

Short URL requires either the BC Math or GMP PHP extensions in order to work.


You can install the package via Composer:

composer require ashallendesign/short-url

Publish the Config and Migrations

You can then publish the package’s config file and database migrations by using the following command:

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

Migrate the Database

This package contains two migrations that add two new tables to the database: short_urls and short_url_visits. To run these migrations, simply run the following command:

php artisan migrate


Building Shortened URLs

The quickest way to get started with creating a shortened URL is by using the snippet below. The ->make() method returns a ShortURL model that you can grab the shortened URL from.

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

$shortURLObject = $builder->destinationUrl('')->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, 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('')->urlKey('custom-key')->make();
$shortURL = $shortURLObject->default_short_url;

// Short URL:

Note: All of the URL keys are unique, so you cannot use a key that already exists in the database for another shortened URL.

Using the Shortened URLs

Default Route and Controller

By default, the shortened URLs that are created use the package’s route and controller. The routes use the following structure:{urlKey}. This route uses the single-use controller that is found at \AshAllenDesign\ShortURL\Controllers\ShortURLController.

Custom Route

You may wish to use a different routing structure for your shortened URLs other than the default URLs that are created. For example, you might want to use{urlKey} or{urlKey}. You can customize this to suit the needs of your project.

To use the custom routing all you need to do is add a web route to your project that points to the ShortURLController and uses the {shortURLKey} field.

The example below shows how you could add a custom route to your web.php file to use the shortened URLs:

Route::get('/custom/{shortURLKey}', '\AshAllenDesign\ShortURL\Controllers\ShortURLController');


Customizing the Default Route

The package comes with a route that you can use for your short URLs. By default, this route is /short/{shortURLKey}.

You might want to keep using this default route but change the /short/ prefix to something else. To do this, you can change the prefix field in the config.

For example, to change the default short URL to /s, you could change the config value like so:

'prefix' => 's',

There are a lot more customization options in this package, that you can visit on Github.

Published at : 27-12-2021

Author : Rizwan Aslam
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