pushery / polyslug-for-laravel
Polymorphic, multilingual routable identity for Eloquent — leak-safe IDs, per-locale slugs, and hreflang built in.
Requires
- php: ^8.4
- ext-ctype: *
- ext-hash: *
- ext-json: *
- ext-mbstring: *
- illuminate/collections: ^13.0
- illuminate/console: ^13.0
- illuminate/container: ^13.0
- illuminate/contracts: ^13.0
- illuminate/database: ^13.0
- illuminate/filesystem: ^13.0
- illuminate/http: ^13.0
- illuminate/routing: ^13.0
- illuminate/support: ^13.0
- illuminate/view: ^13.0
- sqids/sqids: ^0.5.0
- symfony/console: ^7.0 || ^8.0
- symfony/http-foundation: ^7.0 || ^8.0
- symfony/http-kernel: ^7.0 || ^8.0
Requires (Dev)
None
Suggests
- ext-bcmath: Needed by Polyslug\Encoders\SqidsEncoder unless ext-gmp is loaded: Sqids computes with one of the two.
- ext-gmp: Needed by Polyslug\Encoders\SqidsEncoder unless ext-bcmath is loaded: Sqids computes with one of the two.
- laravel/head: Lets a Polyslug model describe its own <head>: Head::polyslug($model) writes the canonical URL, the reciprocal hreflang set, the Open Graph locale set and the robots directive from the model's own slug data. Needs Laravel 13.17+, which is laravel/head's own floor - Polyslug itself needs only 13.0.
- pushery/sqlens-for-laravel: Reads the #[NoSqlOnDriver] attributes on the migrations, which say on which engine a migration runs no SQL and why. Without it they are never read.
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v0.31.0
- v0.30.3
- v0.30.2
- v0.30.1
- v0.30.0
- v0.29.0
- v0.28.0
- v0.27.1
- v0.27.0
- v0.26.0
- v0.25.0
- v0.24.0
- v0.23.0
- v0.22.1
- v0.22.0
- v0.21.0
- v0.20.1
- v0.20.0
- v0.19.0
- v0.18.9
- v0.18.8
- v0.18.7
- v0.18.6
- v0.18.5
- v0.18.4
- v0.18.3
- v0.18.2
- v0.18.1
- v0.18.0
- v0.17.1
- v0.17.0
- v0.16.0
- v0.15.0
- v0.14.0
- v0.13.0
- v0.12.0
- v0.11.0
- v0.10.0
- v0.9.0
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.1
- v0.5.0
- v0.4.0
- v0.3.0
- v0.2.1.x-dev
- v0.2.1
- v0.2.0
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2026-11-28 11:37:37 UTC
README
Polyslug
Polymorphic, multilingual routable identity for Eloquent. Pretty URLs that are safe to expose, safe to rename, and correct across languages — with leak-free IDs, self-healing canonical redirects, and hreflang built in.
/blog/laravel-routing-explained_aB3xK
└──────── slug ─────────┘ └id─┘
changeable, localized, SEO-friendly stable, opaque, resolves the model
A slug should be free to change. A URL should never break. Those two goals usually fight each other. Polyslug settles the fight by splitting a URL into two independent parts — a human-readable slug (pretty, per-locale, editable) and a stable opaque identity (an encoded token that resolves the model). Rename the slug all you like: the identity still resolves, and old URLs redirect themselves to the new canonical one.
Install
composer require pushery/polyslug-for-laravel php artisan migrate
Mark a model as sluggable, point a route at it, and you are done:
namespace App\Models; #[Polyslug(source: 'title')] class Page extends Model implements Sluggable { use HasPolyslug; } Route::polyslug('/pages/{page}', [PageController::class, 'show'])->name('pages.show');
Rename the page and the old URL 301s to the new one, by itself.
Why Polyslug?
- Leak-safe IDs by default. URLs carry an encoded token, not
/pages/1523. No exposed row counts, no enumerable primary keys. The default encoder issues a random, unguessable token per row — the only shipped encoder that makes that claim true end to end. The choice is yours, though: Sqids, UUID, ULID, the raw key, or your own. - URLs as short as you want them. The URL is
{slug}_{token}by default, and both halves are yours to drop or shrink:slugless: truemakes it the token alone (/lists/k3f9dlq7),idLess: truemakes it the slug alone, and the token's length and alphabet are settings — per application or per model. A length that runs out of tokens widens by a character instead of failing, so a short one is a real choice. Need the shortest URL there is? A counted scheme hands out0,1, …z, then00. - Self-healing URLs. Rename freely. A stale slug on a
GET/HEADrequest is301-redirected to the current canonical URL automatically — no redirect tables to hand-maintain, no dead links, no lost link equity. - Multilingual, with hreflang built in. One slug per locale, and a
reciprocal
hreflangset (plusx-default) generated from the same resolver that builds your canonical URL — so they can never drift apart. - Polymorphic routing. Serve every content type — pages, articles, products —
through a single
{type}/{polyslug}route and one registry. - Stable resolution. Route-model binding decodes the identity, not the slug, so
a mistyped or outdated slug still finds the right model (then redirects). An unknown
or malformed token is a clean
404— never a fuzzy match. - Scoped uniqueness. Uniqueness can be scoped per tenant, per locale, per category — whatever columns you name.
- History, events & immutability. Superseded slugs are kept so old URLs keep
resolving; a
SlugChangedevent fires on every change; slugs can be frozen. - Writes its own
<head>vialaravel/head. Install Laravel's<head>package andHead::polyslug($model)fills in the canonical URL, thehreflangset, the Open Graph locales and arobotsdirective for models your visibility gate hides. Optional — Polyslug requires nothing beyond slimilluminate/*components either way. - Proven on the databases you deploy to. The one-current-slug guarantee ships as a functional partial unique index on PostgreSQL and SQLite and as generated key columns on MySQL — two genuinely different mechanisms, and the suite proves both against real PostgreSQL 18 and MySQL 8.4 servers rather than inferring one from the other.
URL examples
| URL | Setting | Suits |
|---|---|---|
/blog/laravel-routing-explained_k3f9dlq7x2mv8bnc |
#[Polyslug(source: 'title')] |
articles and products that get renamed |
/en/products/red-shirt_k3f9dlq7x2mv8bnc and /de/products/rotes-hemd_k3f9dlq7x2mv8bnc |
a slug per locale and 'locale' => ['source' => 'route'] |
multilingual shops: the slug follows the language, the token after _ stays |
/s/x7k2m9q |
#[Polyslug(slugless: true, encoderOptions: ['length' => 7])] |
link shorteners, share links, QR codes |
/about-us |
#[Polyslug(source: 'title', idLess: true)] |
CMS pages whose name is the address |
/docs/getting-started/installation |
idLess: true, scope: 'parent_id' and polyslugParent() |
documentation trees and category hierarchies |
Each one with its route and what it suits: Configuration examples.
Documentation
Full docs at docs.pushery.com/polyslug-for-laravel.
- Installation — requirements, the migration, publishing
- Quick start — a sluggable model and a self-healing route
- Configuration examples — the URL a blog, a multilingual shop, a link shortener and a CMS get, and the settings behind each
- Features — encoders, hreflang, nested paths, sitemaps, short links
- laravel/head — canonical URL, hreflang and indexability in Laravel's
<head>package - Recipes — twelve app shapes wired end to end
- Reference — every config key, option, command, event and contract
Requirements
- PHP 8.4+ (tested on 8.4.27, 8.5.x and 8.6.x)
- Laravel 13+ — 13.17+ only for the optional
laravel/headintegration, which is that package's own floor, not Polyslug's - PostgreSQL 18+, MySQL 8.4+, or SQLite — the uniqueness guarantees are enforced natively on each, and the full suite runs against all three, tested on PostgreSQL 18.7 and MySQL 8.4.13. PostgreSQL and MySQL 8.4 are the databases Laravel Cloud runs (serverless Postgres and MySQL 8.4 LTS).
- The
bcmathorgmpextension, only if you useSqidsEncoder
Security
Please review the security policy and report vulnerabilities privately rather than opening a public issue.
Built by PUSHERY
This package is built and maintained by PUSHERY — a Berlin-based studio building Laravel applications, SaaS products, and open-source tools.
Building a Laravel UI? WireKit is PUSHERY's open-source Livewire component kit. Browse the rest of our work at pushery.com.
License
The MIT License (MIT). See LICENSE for details.