PHP Класс 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', ], ]; }
С версии: 2.0
Автор: Alexander Kochetov ([email protected])
Автор: Paul Klimov ([email protected])
Наследование: extends AttributeBehavior
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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 }

Открытые методы

Метод Описание
init ( )

Защищенные методы

Метод Описание
generateSlug ( array $slugParts ) : string This method is called by [[getValue]] to generate the slug.
generateUniqueSlug ( string $baseSlug, integer $iteration ) : string Generates slug using configured callback or increment of iteration.
getValue ( $event )
isNewSlugNeeded ( ) : boolean Checks whether the new slug generation is needed This method is called by [[getValue]] to check whether the new slug generation is needed.
makeUnique ( string $slug ) : string This method is called by [[getValue]] when [[ensureUnique]] is true to generate the unique slug.
validateSlug ( string $slug ) : boolean Checks if given slug value is unique.

Описание методов

generateSlug() защищенный Метод

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.
Результат string the conversion result.

generateUniqueSlug() защищенный Метод

Generates slug using configured callback or increment of iteration.
protected generateUniqueSlug ( string $baseSlug, integer $iteration ) : string
$baseSlug string base slug value
$iteration integer iteration number
Результат string new slug value

getValue() защищенный Метод

protected getValue ( $event )

init() публичный Метод

public init ( )

isNewSlugNeeded() защищенный Метод

You may override it to customize checking.
С версии: 2.0.7
protected isNewSlugNeeded ( ) : boolean
Результат boolean

makeUnique() защищенный Метод

Calls [[generateUniqueSlug]] until generated slug is unique and returns it.
См. также: getValue
См. также: generateUniqueSlug
С версии: 2.0.7
protected makeUnique ( string $slug ) : string
$slug string basic slug value
Результат string unique slug

validateSlug() защищенный Метод

Checks if given slug value is unique.
protected validateSlug ( string $slug ) : boolean
$slug string slug value
Результат boolean whether slug is unique.

Описание свойств

$attribute публичное свойство

the attribute or list of attributes whose value will be converted into a slug
public $attribute

$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.
public $ensureUnique

$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.
С версии: 2.0.2
public $immutable

$slugAttribute публичное свойство

the attribute that will receive the slug value
public $slugAttribute

$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.
public $uniqueSlugGenerator

$uniqueValidator публичное свойство

configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default [[UniqueValidator]] will be used.
См. также: UniqueValidator
public $uniqueValidator

$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 $value