Support the ongoing development of Laravel.io →
posted 10 years ago
Requests
Last updated 2 years ago.
0

you would do a string replace

URL::action('ArticleController@getView', [$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('ArticleController@getView', [$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 2 years 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 2 years ago.
0

Perfect thnx Kguner and and tariquesani!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

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?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.