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.
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
Perfect thnx Kguner and and tariquesani!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community