Skip to content

Conversation

aldanux
Copy link

@aldanux aldanux commented Oct 13, 2025

This PR adds a new helper to determine whether a given string is a valid, URL-friendly slug.

Motivation

Laravel already offers Str::slug() to generate slugs, but there is no built-in way to validate them. This helper:

  • Improves readability vs. custom regex in each project
  • Encourages consistency across apps and packages
  • Complements the existing is* helpers in Str

Implementation

New methods:

  • Illuminate\Support\Str::isSlug(string $value): bool
  • Illuminate\Support\Stringable::isSlug(): bool

Validation rule (ASCII lowercase, digits, single hyphens, no leading/trailing hyphen, no double hyphens):
^[a-z0-9]+(?:-[a-z0-9]+)*$

Examples

use Illuminate\Support\Str;

Str::isSlug('hello-world');   // true
Str::isSlug('my-slug-123');   // true
Str::isSlug('Hello World');   // false
Str::isSlug('-slug');         // false
Str::isSlug('slug-');         // false
Str::isSlug('slug--test');    // false

str('valid-slug')->isSlug();  // true
str('Not A Slug')->isSlug();  // false

Tests

Added assertions in tests/Support/SupportStrTest.php:

  • Valid slugs (lowercase letters, digits, single hyphens)
  • Invalid cases (uppercase, spaces, leading/trailing/multiple hyphens)
  • Stringable behavior

Examples

public function testItCanDetectValidSlugs(): void
{
    $this->assertTrue(Str::isSlug('hello-world'));
    $this->assertTrue(Str::isSlug('my-slug-123'));
    $this->assertFalse(Str::isSlug('Hello World'));
    $this->assertFalse(Str::isSlug('-slug'));
    $this->assertFalse(Str::isSlug('slug-'));
    $this->assertFalse(Str::isSlug('slug--test'));
}

public function testStringableIsSlug(): void
{
    $this->assertTrue(str('valid-slug')->isSlug());
    $this->assertFalse(str('Not A Slug')->isSlug());
}

Backwards Compatibility

  • Fully backward compatible
  • No new dependencies
  • Naming/style consistent with existing Str::is*() helpers
  • All existing tests pass

Example Usage in Validation

$request->validate([
    'slug' => [
        'required',
        function ($attribute, $value, $fail) {
            if (! Str::isSlug($value)) {
                $fail("The {$attribute} must be a valid slug.");
            }
        },
    ],
]);

Notes

  • Focuses on the common ASCII slug convention used by Str::slug() with - as the separator.
  • If broader i18n slug validation is desired in the future, this API can be extended without breaking changes.

@aldanux aldanux changed the title Add Str::isSlug() and Stringable::isSlug() helper [12.x] Add Str::isSlug() and Stringable::isSlug() helper Oct 13, 2025
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants