PHP Class yii\behaviors\SluggableBehavior
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',
],
];
}
Mostra file
Open project: yiisoft/yii2
Class Usage Examples
Public Properties
Property |
Type |
Description |
|
$attribute |
|
the attribute or list of attributes whose value will be converted into a slug |
|
$ensureUnique |
|
whether to ensure generated slug value to be unique among owner class records.
If enabled behavior will validate slug uniqueness automatically. If validation fails it will attempt
generating unique slug value from based one until success. |
|
$immutable |
|
whether to generate a new slug if it has already been generated before.
If true, the behavior will not generate a new slug even if [[attribute]] is changed. |
|
$slugAttribute |
|
the attribute that will receive the slug value |
|
$uniqueSlugGenerator |
|
slug unique value generator. It is used in case [[ensureUnique]] enabled and generated
slug is not unique. This should be a PHP callable with following signature:
php
function ($baseSlug, $iteration, $model)
{
return uniqueSlug
}
If not set unique slug will be generated adding incrementing suffix to the base slug. |
|
$uniqueValidator |
|
configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default
[[UniqueValidator]] will be used. |
|
$value |
|
the value that will be used as a slug. This can be an anonymous function
or an arbitrary value. If the former, the return value of the function will be used as a slug.
The signature of the function should be as follows,
php
function ($event)
{
return slug
}
|
|
Public Methods
Method |
Description |
|
init ( ) |
|
|
Protected Methods
Method Details
generateSlug()
protected method
You may override it to customize slug generation.
The default implementation calls [[\yii\helpers\Inflector::slug()]] on the input strings
concatenated by dashes (-).
protected generateSlug ( array $slugParts ) : string |
$slugParts |
array |
an array of strings that should be concatenated and converted to generate the slug value. |
return |
string |
the conversion result. |
generateUniqueSlug()
protected method
Generates slug using configured callback or increment of iteration.
getValue()
protected method
isNewSlugNeeded()
protected method
You may override it to customize checking.
makeUnique()
protected method
Calls [[generateUniqueSlug]] until generated slug is unique and returns it.
validateSlug()
protected method
Checks if given slug value is unique.
Property Details
$attribute public_oe property
the attribute or list of attributes whose value will be converted into a slug
$ensureUnique public_oe property
whether to ensure generated slug value to be unique among owner class records.
If enabled behavior will validate slug uniqueness automatically. If validation fails it will attempt
generating unique slug value from based one until success.
$immutable public_oe property
whether to generate a new slug if it has already been generated before.
If true, the behavior will not generate a new slug even if [[attribute]] is changed.
$slugAttribute public_oe property
the attribute that will receive the slug value
$uniqueSlugGenerator public_oe property
slug unique value generator. It is used in case [[ensureUnique]] enabled and generated
slug is not unique. This should be a PHP callable with following signature:
php
function ($baseSlug, $iteration, $model)
{
return uniqueSlug
}
If not set unique slug will be generated adding incrementing suffix to the base slug.
public $uniqueSlugGenerator |
$uniqueValidator public_oe property
configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default
[[UniqueValidator]] will be used.
$value public_oe property
the value that will be used as a slug. This can be an anonymous function
or an arbitrary value. If the former, the return value of the function will be used as a slug.
The signature of the function should be as follows,
php
function ($event)
{
return slug
}