A Laravel package for creating shortened URLs for your web apps.
A Laravel package for creating shortened URLs for your web apps.
A Laravel package that can be used for adding shortened URLs to your existing web app.
The package has been developed and tested to work with the following minimum requirements:
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-urlYou 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"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 migrateThe 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.
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->make();
$shortURL = $shortURLObject->default_short_url;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). 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:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->urlKey('custom-key')
->make();
$shortURL = $shortURLObject->default_short_url;
// Short URL: https://webapp.com/short/custom-keyNote: All of the URL keys are unique, so you cannot use a key that already exists in the database for another shortened URL.
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 Customisation section to see how to customise the default tracking behaviours.
Note: Even if the tracking options (such as track_ip_address) are enabled for a short URL, they won't be recorded
unless the track_visits options is enabled. This can come in handy if you want to enable/disable tracking for a
short URL without needing to individually set each option.
If you want to override whether if 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:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits()
->make();The example below shows how to disable tracking for the URL and override the default config variable:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits(false)
->make();If you want to override whether if 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:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits()
->trackIPAddress()
->make();If you want to override whether if browser name and browser version tracking is enabled or not when creating a shortened
URL, you can use the ->trackBrowser() and ->trackBrowserVersion() methods. This method accepts a boolean
but defaults to true if a parameter is not passed.
The example below shows how to enable browser name tracking for the URL and override the default config variable:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits()
->trackBrowser()
->make();The example below shows how to enable browser version tracking for the URL and override the default config variable:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits()
->trackBrowserVersion()
->make();If you want to override whether if operating system name and operating system version tracking is enabled or not when
creating a shortened URL, you can use the ->trackOperatingSystem() and ->trackOperatingSystemVersion()
methods. These methods accept a boolean but default to true if a parameter is not passed.
The example below shows how to enable operating system name tracking for the URL and override the default config variable:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits()
->trackOperatingSystem()
->make();The example below shows how to enable operating system version tracking for the URL and override the default config variable:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits()
->trackOperatingSystemVersion()
->make();If you want to override whether if device type tracking is enabled or not when creating a shortened URL, you can use the
->trackDeviceType() method. This method accepts a boolean but defaults to true if a parameter is not passed.
The example below shows how to enable device type tracking for the URL and override the default config variable:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits()
->trackDeviceType()
->make();If you want to override whether if referer URL tracking is enabled or not when creating a shortened URL, you can use the
->trackRefererURL() method. This method accepts a boolean but defaults to true if a parameter is not passed.
The example below shows how to enable referer URL tracking for the URL and override the default config variable:
use AshAllenDesign\ShortURL\Classes\Builder;
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->trackVisits()
->trackRefererURL()
->make();There may be times when you want to add your own custom fields to the ShortURL model and store them in the database. For example, you might want to associate the short URL with a tenant, organisation, user, etc.
To do this you can use the beforeCreate method when building your short URL. This method accepts a closure that receives the AshAllenDesign\ShortURL\Models\ShortURL model instance before it's saved to your database.
The example below shows how to add a tenant_id field to the AshAllenDesign\ShortURL\Models\ShortURL model:
use AshAllenDesign\ShortURL\Models\ShortURL;
use AshAllenDesign\ShortURL\Facades\ShortURL as ShortUrlBuilder;
$tenantId = 123;
$shortURL = ShortUrlBuilder::destinationUrl($url)
->beforeCreate(function (ShortURL $model): void {
$model->tenant_id = $tenantId;
})
->make();Please remember that to store custom fields in the database, you'll have to make sure those fields are added to the `short_u
No open issues yet, or sync has not completed.