<!-- Source: @site/projects/polyslug-for-laravel/docs/configuration-examples.md -->
<!-- Canonical: https://docs.pushery.com/polyslug-for-laravel/configuration-examples/ -->

# Configuration examples

> What the URL looks like, what each shape suits, and the settings that make it: a blog, a multilingual shop, a link shortener, a CMS and a documentation tree.

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.

| URL | Shape | Suits |
|---|---|---|
| `/blog/laravel-routing-explained_k3f9dlq7x2mv8bnc` | slug and token, the default | articles and products that get renamed |
| `/en/products/red-shirt_k3f9dlq7x2mv8bnc` | a slug per language, one token | multilingual shops and sites |
| `/s/x7k2m9q` | token only | link shorteners, share links, QR codes |
| `/s/0`, `/s/1` … `/s/z`, `/s/00` | counted token | the shortest URL there is, for public records |
| `/about-us` | slug only | CMS pages whose name is the address |
| `/docs/getting-started/installation` | nested slugs, no token | documentation trees and category hierarchies |

## A blog: slug and token {#blog}

```text
/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.

```php
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;
}
```

```php
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`:

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

## A multilingual shop: a slug per language, one token {#multilingual-shop}

```text
/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.

```php
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:

```php
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:

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

Route::polyslug('/{locale}/products/{product}', [ProductController::class, 'show'])->name('products.show');
```

```php
// 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](https://docs.pushery.com/polyslug-for-laravel/features/multilingual-and-hreflang.md).

## A link shortener: the token alone {#link-shortener}

```text
/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.

```php
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;
}
```

```php
use App\Http\Controllers\ShortUrlController;
use Illuminate\Support\Facades\Route;

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

[Token-only URLs](https://docs.pushery.com/polyslug-for-laravel/features/token-only-urls.md) covers how long and how random the token should be.

## The shortest URL there is: a counted token {#counted-token}

```text
/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.

```php
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](https://docs.pushery.com/polyslug-for-laravel/features/short-links.md).

## A CMS page: the slug alone {#cms-page}

```text
/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.

```php
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;
}
```

```php
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](https://docs.pushery.com/polyslug-for-laravel/features/slug-only-urls.md).

## A documentation tree: nested slugs without a token {#documentation-tree}

```text
/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.

```php
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);
    }
}
```

```php
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](https://docs.pushery.com/polyslug-for-laravel/features/nested-slugs.md).
