Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

short-url

> 后端框架
Open source

A Laravel package for creating shortened URLs for your web apps.

1.4K stars0 likes2 views
WebsiteGitHub

About

A Laravel package for creating shortened URLs for your web apps.

Table of Contents

  • Overview
  • Installation
    • Requirements
    • Install the Package
    • Publish the Config and Migrations
    • Migrate the Database
  • Usage
    • Building Shortened URLs
      • Quick Start
      • Custom Keys
      • Tracking Visitors
        • Enabling Tracking
        • Tracking IP Address
        • Tracking Browser & Browser Version
        • Tracking Operating System & Operating System Version
        • Tracking Device Type
        • Tracking Referer URL
      • Custom Short URL Fields
      • Single Use
      • Enforce HTTPS
      • Forwarding Query Parameters
      • Redirect Status Code
      • Activation and Deactivation Times
      • Using a Custom Seed
      • Facade
      • Conditionals
    • Using the Shortened URLs
      • Default Route and Controller
      • Custom Route
    • Tracking
    • Customisation
      • Disabling the Default Route
      • Default URL Key Length
      • Tracking Visits
        • Default Tracking
        • Tracking Fields
      • Config Validation
      • Custom Database Connection
      • Specifying the Key Generator
      • Specifying the User Agent Parser
      • Specifying the Allowed URL Schemes
    • Helper Methods
      • Visits
      • Find by URL Key
      • Find by Destination URL
      • Tracking Enabled
      • Tracked Fields
    • Events
      • Short URL Visited
    • Model Factories
  • Testing
  • Security
  • Contribution
  • Credits
  • Changelog
  • Upgrading
  • License

Overview

A Laravel package that can be used for adding shortened URLs to your existing web app.

Installation

Requirements

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

  • PHP 8.2
  • Laravel 10.0

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

Install the Package

You can install the package via Composer:

bash
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:

bash
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:

bash
php artisan migrate

Usage

Building Shortened URLs

Quick Start

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.

php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)
    ->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). 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:

php
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-key

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

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 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.

Enabling Tracking

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:

php
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:

php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)
    ->destinationUrl('https://destination.com')
    ->trackVisits(false)
    ->make();
Tracking IP Address

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:

php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)
    ->destinationUrl('https://destination.com')
    ->trackVisits()
    ->trackIPAddress()
    ->make();
Tracking Browser & Browser Version

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:

php
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:

php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)
    ->destinationUrl('https://destination.com')
    ->trackVisits()
    ->trackBrowserVersion()
    ->make();
Tracking Operating System & Operating System Version

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:

php
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:

php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)
    ->destinationUrl('https://destination.com')
    ->trackVisits()
    ->trackOperatingSystemVersion()
    ->make();
Tracking Device Type

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:

php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)
    ->destinationUrl('https://destination.com')
    ->trackVisits()
    ->trackDeviceType()
    ->make();
Tracking Referer URL

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:

php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)
    ->destinationUrl('https://destination.com')
    ->trackVisits()
    ->trackRefererURL()
    ->make();

Custom Short URL Fields

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:

php
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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPhacktoberfestlaravel

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架