Support the ongoing development of Laravel.io →
posted 8 years ago
Requests
Last updated 4 months ago.
0
Solution

you would do a string replace

URL::action('[email protected]', [$article->id, str_replace(' ', '-', $article->title)])

I have a method on my models to create slugs, it handles other cases such as multiple spaces, international characters, lowercasing, and strips punctuation and special characters.

public function slug()
{
    $clean = $this->title;
    setlocale(LC_ALL, 'en_US.UTF8');

    $delimiter = '-';

    $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
    $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
    $clean = strtolower(trim($clean, $delimiter));

    return $clean;
}

I got the method from the following url: http://cubiq.org/the-perfect-php-clean-url-generator

Then you would use it like:

URL::action('[email protected]', [$article->id, $article->slug()])

Also, if your article names are unique, you can save the slug to the database and drop the id from the url, and search for article by slug instead of id. Make sure the slug is also unique.

Last updated 4 months ago.
0

What you want is a slug. Consider using https://github.com/cviebrock/eloquent-sluggable or if you want to code your own use Str::slug to create slug from Title - the syntax is Str::slug($title,$separator = '-')

HTH

Last updated 4 months ago.
0

Perfect thnx Kguner and and tariquesani!

Last updated 4 months ago.
0

Sign in to participate in this thread!

LoadForge

Your banner here too?

brownpixel brownpixel Joined 15 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.