short-url

repository·master·Indexed 23 days ago

https://github.com/ash-jc-allen/short-url

A Laravel package for generating, managing, and tracking shortened URLs. It features a fluent Builder class for creating URLs with custom keys, expiration dates, and granular visitor tracking (IP, browser, OS, device type). The package supports custom route structures, single-use URLs, and a beforeCreate callback for adding custom model fields. Requires PHP 8.2, Laravel 10.0, and either the BC Math or GMP extension.

Tokens
8.3K
Snippets
19
Records
51
Agent score
80%

What's inside short-url

  1. Configure Visitor Tracking

    master

    Tracking is enabled by default. You can override tracking settings for individual URLs during creation using the Builder methods.

    Important: Even if specific fields (like track_ip_address) are enabled, they will not be recorded unless trackVisits() is also enabled (or set to true).

    Available Tracking Methods

    • trackVisits(bool $enabled = true): Enables/disables overall visit recording.
    • trackIPAddress(bool $enabled = true): Enables/disables IP address tracking.
    • trackBrowser(bool $enabled = true): Enables/disables browser name tracking.
    • trackBrowserVersion(bool $enabled = true): Enables/disables browser version tracking.
    • trackOperatingSystem(bool $enabled = true): Enables/disables OS name tracking.
    • trackOperatingSystemVersion(bool $enabled = true): Enables/disables OS version tracking.
    • trackDeviceType(bool $enabled = true): Enables/disables device type tracking.
    • trackRefererURL(bool $enabled = true): Enables/disables referer URL tracking.
    // Example: Enabling specific tracking fields
    use AshAllenDesign\ShortURL\Classes\Builder;
    
    $shortURLObject = app(Builder::class)
        ->destinationUrl('https://destination.com')
        ->trackVisits()
        ->trackIPAddress()
        ->make();
  2. Listen for Short URL Visited Events

    master

    The package dispatches the AshAllenDesign\ShortURL\Events\ShortURLVisited event every time a short URL is visited.

    Note on 301 Redirects: If you use a 301 (Permanent) redirect status code, browsers may cache the destination and skip the short URL on subsequent visits. This means the event might not fire for repeat visitors. For reliable event dispatching, use a 302 (Temporary) redirect.

  3. Publish migrations for v5.0.0 and later

    master

    Starting from v5.0.0, Short URL no longer automatically loads database migrations via the service provider. You must manually publish the migrations to your project's database/migrations folder to ensure they are part of your application's migration lifecycle.

    Run the following command in your project root to publish them:

    php artisan vendor:publish --tag="short-url-migrations"
  4. Quick Start: Building Shortened URLs

    master

    The fastest way to create a shortened URL is by using the Builder class. The ->make() method returns a ShortURL model instance, from which you can retrieve the default_short_url property.

    use AshAllenDesign\ShortURL\Classes\Builder;
    
    $shortURLObject = app(Builder::class)
        ->destinationUrl('https://destination.com')
        ->make();
    
    $shortURL = $shortURLObject->default_short_url;
  5. Add activation columns to short_urls table (v3.0.0)

    master

    Short URL v3.0.0 introduced two new columns to the short_urls table: activated_at and deactivated_at.

    To apply these changes, run:

    php artisan migrate

    If you wish to customize the migrations before running them, publish them first using:

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

    Note on Data: When running these migrations, existing short URLs will have today's date automatically populated in the activated_at column. The deactivated_at column will remain null, meaning existing URLs will remain active indefinitely.

  6. Upgrading from 7.* to 8.0.0

    master

    When upgrading to Short URL v8.0.0, note the following breaking changes and requirements:

    Minimum Laravel Version

    Short URL v8.0.0 requires a minimum of Laravel 8.0.

    Minimum PHP Version

    Short URL v8.0.0 requires a minimum of PHP 7.3.

    Tracking Field Behavior

    Undetectable tracking fields are now stored as null instead of false in the database.

    Type Safety and Signatures

    • Property types have been added to the codebase.
    • Several method signatures have changed. Ensure you check your implementations against the new public API.