To use SluggableBehavior, insert the following code to your ActiveRecord class:
php
use yii\behaviors\SluggableBehavior;
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
'slugAttribute' => 'slug',
],
];
}
By default, SluggableBehavior will fill the slug attribute with a value that can be used a slug in a URL
when the associated AR object is being validated.
Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore
not be validated, i.e. the slug attribute should not appear in the [[\yii\base\Model::rules()|rules()]] method of the model.
If your attribute name is different, you may configure the [[slugAttribute]] property like the following:
php
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'slugAttribute' => 'alias',
],
];
}