vlados/laravel-unique-urls
Composer 安装命令:
composer require vlados/laravel-unique-urls
包简介
A package for using and generating unique urls for each Eloquent model in Laravel
README 文档
README
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:doctorcommand 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
- Configuration — config options and defaults
- Usage — model setup, routes, multi-language, Livewire
- Bulk Operations — batch generation, global toggles, performance
- Artisan Commands —
urls:generateandurls:doctor - Testing — assertion helpers and example tests
- Troubleshooting — common issues and fixes
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.
统计信息
- 总下载量: 1.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-07