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', ], ]; }
Since: 2.0
Author: Alexander Kochetov ([email protected])
Author: Paul Klimov ([email protected])
Inheritance: extends AttributeBehavior
ファイルを表示 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 Description
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.

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.
protected generateUniqueSlug ( string $baseSlug, integer $iteration ) : string
$baseSlug string base slug value
$iteration integer iteration number
return string new slug value

getValue() protected method

protected getValue ( $event )

init() public method

public init ( )

isNewSlugNeeded() protected method

You may override it to customize checking.
Since: 2.0.7
protected isNewSlugNeeded ( ) : boolean
return boolean

makeUnique() protected method

Calls [[generateUniqueSlug]] until generated slug is unique and returns it.
See also: getValue
See also: generateUniqueSlug
Since: 2.0.7
protected makeUnique ( string $slug ) : string
$slug string basic slug value
return string unique slug

validateSlug() protected method

Checks if given slug value is unique.
protected validateSlug ( string $slug ) : boolean
$slug string slug value
return boolean whether slug is unique.

Property Details

$attribute public_oe property

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

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

$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.
Since: 2.0.2
public $immutable

$slugAttribute public_oe property

the attribute that will receive the slug value
public $slugAttribute

$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.
See also: UniqueValidator
public $uniqueValidator

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