Skip to main content

Configuration examples

Polyslug builds a URL from two parts: a slug a person can read, and a token that finds the record. Each example below keeps more of one part or the other. Pick the shape by what the URL has to do; every section shows the URL, what the shape suits and the code that sets it up.

URLShapeSuits
/blog/laravel-routing-explained_k3f9dlq7x2mv8bncslug and token, the defaultarticles and products that get renamed
/en/products/red-shirt_k3f9dlq7x2mv8bnca slug per language, one tokenmultilingual shops and sites
/s/x7k2m9qtoken onlylink shorteners, share links, QR codes
/s/0, /s/1 … /s/z, /s/00counted tokenthe shortest URL there is, for public records
/about-usslug onlyCMS pages whose name is the address
/docs/getting-started/installationnested slugs, no tokendocumentation trees and category hierarchies

A blog: slug and token​

/blog/laravel-routing-explained_k3f9dlq7x2mv8bnc
└──────── slug ─────────┘ └──── token ───┘

The default shape. The slug before the _ comes from the title and may change at any time. The token after it is random, 16 characters from 0-9a-z unless you set otherwise, and it is what finds the post. Rename the post and the old URL answers with a 301 to the new one.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Polyslug\Attributes\Polyslug;
use Polyslug\Concerns\HasPolyslug;
use Polyslug\Contracts\Sluggable;

#[Polyslug(source: 'title')]
class Post extends Model implements Sluggable
{
use HasPolyslug;
}
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;

Route::polyslug('/blog/{post}', [PostController::class, 'show'])->name('posts.show');

A shorter token for every model is one setting, and it gives /blog/laravel-routing-explained_k3f9dlq7:

// config/polyslug.php
'random_token' => ['length' => 8],

A multilingual shop: a slug per language, one token​

/en/products/red-shirt_k3f9dlq7x2mv8bnc
/de/products/rotes-hemd_k3f9dlq7x2mv8bnc

Each language has its own slug, and the token after the _ is the same in every language, because it names the product, not the words. A link copied from the German shop still finds the product, and the canonical redirect answers with the slug of the language in the URL.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Polyslug\Attributes\Polyslug;
use Polyslug\Concerns\HasPolyslug;
use Polyslug\Contracts\Sluggable;

#[Polyslug(source: 'name')]
class Product extends Model implements Sluggable
{
use HasPolyslug;
}

The slug of the application's locale comes from name. Give the product its slug in another language:

use App\Models\Product;

$product = Product::query()->firstOrFail();

$product->setSlug('de', 'Rotes Hemd'); // stored as "rotes-hemd"

Put the locale into the route, and tell Polyslug to read it from there:

use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Route;

Route::polyslug('/{locale}/products/{product}', [ProductController::class, 'show'])->name('products.show');
// config/polyslug.php
'locale' => [
'source' => 'route',
'route_param' => 'locale',
'missing' => 'fallback',
'fallback_locale' => null,
],

With source set to route, the canonical-redirect middleware compares the slug against the locale in the URL rather than the application's locale, so a German URL is never redirected to an English slug. The hreflang links come from the same resolver as the URL; see Multilingual slugs and hreflang.

/s/x7k2m9q

With slugless: true the URL carries no slug, only the token. Renaming the record cannot change its URL, which is what a printed, shared or spoken link needs. The token's length is set per model here, and a length whose space fills up widens by a character instead of failing.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Polyslug\Attributes\Polyslug;
use Polyslug\Concerns\HasPolyslug;
use Polyslug\Contracts\Sluggable;

#[Polyslug(slugless: true, encoderOptions: ['length' => 7])]
class ShortUrl extends Model implements Sluggable
{
use HasPolyslug;
}
use App\Http\Controllers\ShortUrlController;
use Illuminate\Support\Facades\Route;

Route::polyslug('/s/{shortUrl}', [ShortUrlController::class, 'show'])->name('short-urls.show');

Token-only URLs covers how long and how random the token should be.

The shortest URL there is: a counted token​

/s/0 /s/1 … /s/z /s/00

SequentialTokenEncoder hands out tokens in order, starting at one character. The URL is as short as it can be, and it is not secret: anyone can count through the records. Use it where every record is public anyway, and keep the random token wherever a URL must not be guessed.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Polyslug\Attributes\Polyslug;
use Polyslug\Concerns\HasPolyslug;
use Polyslug\Contracts\Sluggable;
use Polyslug\Encoders\SequentialTokenEncoder;

#[Polyslug(slugless: true, encoder: SequentialTokenEncoder::class)]
class ShortCode extends Model implements Sluggable
{
use HasPolyslug;
}

For a QR code on a flyer, a descriptive URL can keep its slug and carry a short link beside it: $model->shortLink() gives a /go/… link that always redirects to the current URL. See Short links.

A CMS page: the slug alone​

/about-us

With idLess: true the URL is the slug alone, and the slug becomes the identity. Polyslug keeps it unique and keeps a retired slug reserved, so renaming one page cannot hand its published address to another.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Polyslug\Attributes\Polyslug;
use Polyslug\Concerns\HasPolyslug;
use Polyslug\Contracts\Sluggable;

#[Polyslug(source: 'title', idLess: true)]
class Page extends Model implements Sluggable
{
use HasPolyslug;
}
use App\Http\Controllers\PageController;
use Illuminate\Support\Facades\Route;

Route::polyslug('/{page}', [PageController::class, 'show'])->name('pages.show');

A route at the root competes with every other route of the application. Setting polyslug.reserved.from_routes to true keeps a generated slug from taking the first segment of a route you registered. See Slug-only URLs.

A documentation tree: nested slugs without a token​

/docs/getting-started/installation

Each segment is the slug of one record, composed from its parents' current slugs whenever a URL is built. Moving or renaming a section changes every URL below it at once, and the old paths redirect. With idLess: true the path carries no token at all.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Polyslug\Attributes\Polyslug;
use Polyslug\Concerns\HasPolyslug;
use Polyslug\Contracts\Sluggable;

#[Polyslug(source: 'title', scope: 'parent_id', idLess: true)]
class DocPage extends Model implements Sluggable
{
use HasPolyslug;

public function polyslugParent(): ?Sluggable
{
return $this->parent_id === null ? null : self::find($this->parent_id);
}
}
use App\Http\Controllers\DocPageController;
use Illuminate\Support\Facades\Route;

Route::polyslug('/docs/{docPage}', [DocPageController::class, 'show'])->where('docPage', '.*');

scope: 'parent_id' lets two sections each have an installation page. See Nested slugs.