Reputation: 1574
In yii2 framework, which is the similar function as url::title()
in Kohana? An example would be
$input_title = ' __Ecléçtic__ title\'s entered by cràzed users- ?> ';
echo url::title($input_title, '_');
Generates:
eclectic_titles_entered_by_crazed_users
Upvotes: 1
Views: 80
Reputation: 3567
Well you do have sluggable behavior built in to yii. Docs.
In your model you have to update the behaviors:
use yii\behaviors\SluggableBehavior;
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
],
];
}
I always use the id as part of the slug, to ensure uniqueness.
'urlManager' => [
'rules' => [
'<id:\d+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',
]
]
Upvotes: 0
Reputation: 1574
Since I have received no suggestion on what Yii2 method to use I wrote this one on my custom helpers. The Slugify package suggested is also good but one function was enough for me.
public static function title($title, $separator = '-')
{
$separator = ($separator === '') ? '' : '-';
// Remove all characters that are not the separator, a-z, 0-9, or whitespace and arabic lang
$title = preg_replace ( "/&([\x{0600}-\x{06FF}a-zA-Z])(uml|acute|grave|circ|tilde|ring),/u", "", $title );
$title = preg_replace ( "/[^\x{0600}-\x{06FF}a-zA-Z0-9_ .-]/u", "", $title );
// Remove all characters that are not the separator, a-z, 0-9, or whitespace
// $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
// Trim separators from the beginning and end
return trim($title, $separator);
}
Upvotes: 0
Reputation: 2012
This project will help you.
Code example:
$slugify->activateRuleset('esperanto');
echo $slugify->slugify('serĉi manĝi'); // sercxi-mangxi
Upvotes: 1