Consent

This site uses third party services that need your consent.

Skip to content
Steven Roland

Generating Randomized Slugs in Laravel: A Step-by-Step Guide

When building web applications with Laravel, you might encounter scenarios where you need to generate unique and memorable slugs for URLs. A common approach is to create slugs using a combination of two simple words, such as "warm-cavern" or "wild-ocelot." In this blog post, we'll walk through how to achieve this in Laravel using a simple and efficient method.

Why Use Randomized Slugs?

Randomized slugs are not only unique but also human-readable, making them ideal for URLs. They can enhance user experience by providing memorable links and can be used in various applications, such as blog posts, product pages, or user profiles.

Step-by-Step Implementation

Let's dive into the implementation of generating two-word slugs in Laravel.

Step 1: Create a List of Words

First, you'll need two lists of words: one for adjectives and another for nouns. These lists will serve as the source for generating the slugs.

$adjectives = [
    'warm', 'wild', 'brave', 'calm', 'fast', 'gentle', 'happy', 'kind', 'lucky', 'proud'
];

$nouns = [
    'cavern', 'ocelot', 'forest', 'river', 'mountain', 'valley', 'ocean', 'desert', 'island', 'jungle'
];

Feel free to expand these lists with more words to increase the variety and randomness of the generated slugs.

Step 2: Randomly Select Words

Using Laravel's helper functions, you can randomly select one word from each list. The array_rand() function is perfect for this task.

$adjective = $adjectives[array_rand($adjectives)];
$noun = $nouns[array_rand($nouns)];

This code snippet picks a random adjective and a random noun from the respective arrays.

Step 3: Concatenate the Words

Combine the selected words with a hyphen to form the slug. Laravel's Str::slug() function ensures that the generated slug is URL-friendly.

use Illuminate\Support\Str;

$slug = Str::slug($adjective . '-' . $noun);

The Str::slug() function formats the string to be suitable for URLs, although in this case, it simply concatenates the words with a hyphen.

Step 4: Implement in a Controller

Here's how you can implement the entire process in a Laravel controller or service:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Str;

class SlugController extends Controller
{
    public function generateSlug()
    {
        $adjectives = [
            'warm', 'wild', 'brave', 'calm', 'fast', 'gentle', 'happy', 'kind', 'lucky', 'proud'
        ];

        $nouns = [
            'cavern', 'ocelot', 'forest', 'river', 'mountain', 'valley', 'ocean', 'desert', 'island', 'jungle'
        ];

        $adjective = $adjectives[array_rand($adjectives)];

        $noun = $nouns[array_rand($nouns)];

        $slug = Str::slug($adjective . '-' . $noun);

        return $slug;
    }
}

How Many Unique Combinations Can You Create?

The number of unique combinations you can generate depends on the number of adjectives and nouns in your lists. By multiplying the number of adjectives by the number of nouns, you can determine the total number of unique slugs possible. Here are some examples:

  • 5 Adjectives and 5 Nouns: 25 unique combinations

  • 5 Adjectives and 10 Nouns: 50 unique combinations

  • 5 Adjectives and 15 Nouns: 75 unique combinations

  • 10 Adjectives and 5 Nouns: 50 unique combinations

  • 10 Adjectives and 10 Nouns: 100 unique combinations

  • 10 Adjectives and 15 Nouns: 150 unique combinations

  • 15 Adjectives and 5 Nouns: 75 unique combinations

  • 15 Adjectives and 10 Nouns: 150 unique combinations

  • 15 Adjectives and 15 Nouns: 225 unique combinations

These combinations illustrate how expanding the lists of adjectives and nouns can significantly increase the number of unique slugs you can generate. By increasing the variety of words in each list, you can create more distinctive and memorable slugs for your application.

Conclusion

Generating randomized slugs in Laravel is a straightforward process that can significantly enhance the usability and memorability of your application's URLs. By following the steps outlined above, you can create unique and descriptive slugs that are both functional and user-friendly. Whether you're building a blog, an e-commerce platform, or any other web application, this method offers a simple yet effective solution for slug generation.

Feel free to customize the word lists and selection logic to suit your specific needs, and enjoy creating memorable slugs for your Laravel application!

More posts

How to Identify and Deal with a Narcissist

Learn to identify narcissists by their grandiosity, need for admiration, and lack of empathy. Manage relationships by setting boundaries, communicating clearly, and seeking support. Prioritize your well-being and consider professional help if needed.

Building a Simple Marketing Website with Laravel Folio

Learn to build a simple marketing website using Laravel Folio. Set up a Laravel project, install Folio, create pages with Blade templates, and maintain consistency with layouts. Quickly create dynamic content for your marketing site.