LARAVEL KEBAB AND SLUG STRING HELPER METHODS

July 28th, 2019 by Kevin Pimentel

I love the Laravel framework, it’s my favorite. The documentation is great, I recommend reading it through like a book. I was looking through the API documentation, as I tend to do. 


I needed to generate a slug from my blog post titles automatically. I knew that Laravel had a helper method for returning URL friendly strings. I navigated on over to the helpers page and I noticed the camel, snake, and kebab methods.


I didn’t stop to read the actual documentation. I just saw the kebab method and thought “kebab should do the trick!” and I plugged it into my code:


public function setTitleAttribute($value)
{
    $this->attributes['title'] = ucwords($value);
    $this->attributes['slug'] = Str::kebab($value);
}

Here’s the string I plugged in:

Str::kebab(‘Laravel Blog Using TDD Part 2: View and Create Blog Posts’);

I was expecting this:

laravel-blog-using-tdd-part-2-view-and-create-blog-posts

Instead I got this:

laravel-blog-using-tdd-part-2:-view-and-create-blog-posts


I was a bit confused at first, so I looked into the code to see what was going on under the hood.


The kebab method calls snake:

public static function kebab($value)
{
    return static::snake($value, '-');
}

Then snake does a bunch of science, but it doesn’t look like it’s concerned with URL friendly strings.

public static function snake($value, $delimiter = '_')
{
    $key = $value;

    if (isset(static::$snakeCache[$key][$delimiter])) {
        return static::$snakeCache[$key][$delimiter];
    }

    if (! ctype_lower($value)) {
        $value = preg_replace('/\s+/u', '', ucwords($value));

        $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
    }

    return static::$snakeCache[$key][$delimiter] = $value;
}

I looked at the slug method in the documentation and the first thing it mentions is that it returns a URL friendly string. Ooops.

public static function slug($title, $separator = '-', $language = 'en')
{
    $title = $language ? static::ascii($title, $language) : $title;

    // Convert all dashes/underscores into separator
    $flip = $separator === '-' ? '_' : '-';

    $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);

    // Replace @ with the word 'at'
    $title = str_replace('@', $separator.'at'.$separator, $title);

    // Remove all characters that are not the separator, letters, numbers, or whitespace.
    $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title));

    // Replace all separator characters and whitespace by a single separator
    $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);

    return trim($title, $separator);
}

Do you even read?

Long story short, actually reading the documentation is helpful. The slug method is meant for URL friendly strings. The kebab method isn’t concerned with URL friendly strings. Diving into the actual framework code is really helpful in gaining a deeper understanding of how things are working. This could have been avoided if I just read the documentation but looking at the framework code is quick and useful as well.

Kevin Pimentel

There are two types of people in the world: those that code, and those that don’t. I said that! Quote me. My name is Kevin and I’m one of the ones that codes.