Skip to content

vlados/laravel-unique-urls

Repository files navigation

Unique Urls for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads PHP Insights PHPStan

Generate unique, prefix-free URLs for any Eloquent model — blogs, e-commerce, multi-language platforms.

PHP 8.2+ | Laravel 11, 12

Features

  • Auto-trim slashes to prevent 404 errors
  • Multi-language URLs (distinct slugs per locale, not just prefixes)
  • Automatic redirects when URLs change (301 by default)
  • Hierarchical URLs via parent relationships (e.g. category/product)
  • Livewire full-page component support (FQCN and SFC names)
  • Batch generation with progress tracking for large datasets
  • Slug validation and reserved-slug protection
  • urls:doctor command for configuration health checks

Installation

composer require vlados/laravel-unique-urls
php artisan vendor:publish --tag="laravel-unique-urls-migrations"
php artisan migrate

Quick Start

Add the trait to your model and define how URLs are built:

use Vlados\LaravelUniqueUrls\HasUniqueUrls;

class Product extends Model
{
    use HasUniqueUrls;

    public function urlStrategy($language, $locale): string
    {
        return Str::slug($this->name, '-', $locale);
    }

    public function urlHandler(): array
    {
        return [
            'controller' => ProductController::class,
            'method'     => 'view',
            'arguments'  => [],
        ];
    }
}

Register the catch-all route at the end of routes/web.php:

Route::get('{urlObj}', [\Vlados\LaravelUniqueUrls\LaravelUniqueUrlsController::class, 'handleRequest'])
    ->where('urlObj', '.*');

Access URLs on any model instance:

$product->relative_url;  // "my-product"
$product->absolute_url;  // "https://example.com/my-product"
$product->getSlug('en'); // slug for a specific language

Documentation

Changelog

See CHANGELOG.md for release history.

Contributing

This project follows Conventional Commits.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). See LICENSE.md for details.

About

A package for using and generating unique urls for each Eloquent model in Laravel

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors

Languages