How to generate slug in Laravel?
Slug is simplified version of a string for making URL-friendly application. it generally removes non-URL-friendly characters (spaces, accented letters, ampersands etc.). The resulted string can be used as an identifier for a particular resource. Laravel provides string helper str_slug() by which we can easily slug a string. It also good for SEO friendly UR.
Let's show with an example how you can use slug in your application.
1. Let's make a route first:
Route::get('/slug',['uses'=>'UserController@getSlug','as'=>'slug']);
2. Let's make the getSlug() method:
public function getSlug(){$mySlug = str_slug('It is an example post');print_r($mySlug);}
Now if you visit the route you'll get the following output:
it-is-an-example-post
See how easily you can make a SEO friendly url for your application using laravel helper.
So that's it for today. Hope you like the tutorial. Stay tune for my next Tut. Till then Good Bye!
0 comments:
Post a Comment