没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\behaviors\SluggableBehavior

继承yii\behaviors\SluggableBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\BaseObject
实现yii\base\Configurable
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/behaviors/SluggableBehavior.php

SluggableBehavior automatically fills the specified attribute with a value that can be used a slug in a URL.

Note: This behavior relies on php-intl extension for transliteration. If it is not installed it falls back to replacements defined in yii\helpers\Inflector::$transliteration.

To use SluggableBehavior, insert the following code to your ActiveRecord class:

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 rules() method of the model.

If your attribute name is different, you may configure the $slugAttribute property like the following:

public function behaviors()
{
    return [
        [
            'class' => SluggableBehavior::className(),
            'slugAttribute' => 'alias',
        ],
    ];
}

公共属性

隐藏继承的属性

属性类型描述被定义在
$attribute string|array|null The attribute or list of attributes whose value will be converted into a slug or null meaning that the $value property will be used to generate a slug. yii\behaviors\SluggableBehavior
$attributes array 属性列表,属性的值将由 $value 自动填充。 数组的键是 ActiveRecord 的事件,属性就是更新于这些事件之上, 数组的值就是要更新的属性。 你可以用字符串来表示一个单独的属性也可以用一个数组来表示一系列属性。比如, `php [ ActiveRecord::EVENT_BEFORE_INSERT => ['attribute1', 'attribute2'], ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2', ] ` yii\behaviors\AttributeBehavior
$ensureUnique boolean Whether to ensure generated slug value to be unique among owner class records. yii\behaviors\SluggableBehavior
$immutable boolean Whether to generate a new slug if it has already been generated before. yii\behaviors\SluggableBehavior
$owner yii\base\Component|null The owner of this behavior yii\base\Behavior
$preserveNonEmptyValues boolean 是否保留非空的属性值 yii\behaviors\AttributeBehavior
$skipOnEmpty boolean Whether to skip slug generation if $attribute is null or an empty string. yii\behaviors\SluggableBehavior
$skipUpdateOnClean boolean $owner 没有更新的时候是否跳过这个行为 yii\behaviors\AttributeBehavior
$slugAttribute string The attribute that will receive the slug value yii\behaviors\SluggableBehavior
$uniqueSlugGenerator callable Slug unique value generator. yii\behaviors\SluggableBehavior
$uniqueValidator array Configuration for slug uniqueness validator. yii\behaviors\SluggableBehavior
$value callable|string|null The value that will be used as a slug. yii\behaviors\SluggableBehavior

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
attach() Attaches the behavior object to the component. yii\base\Behavior
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
detach() Detaches the behavior object from the component. yii\base\Behavior
evaluateAttributes() 计算属性的值并分配给当前属性。 yii\behaviors\AttributeBehavior
events() Declares event handlers for the $owner's events. yii\behaviors\AttributeBehavior
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\behaviors\SluggableBehavior

受保护的方法

隐藏继承的方法

方法描述被定义在
generateSlug() This method is called by getValue() to generate the slug. yii\behaviors\SluggableBehavior
generateUniqueSlug() Generates slug using configured callback or increment of iteration. yii\behaviors\SluggableBehavior
getValue() 返回给当前属性准备的值。 该方法在 evaluateAttributes() 里调用。 它的返回值将会设置到对应触发事件的属性上。 yii\behaviors\SluggableBehavior
isEmpty() Checks if $slugPart is empty string or null. yii\behaviors\SluggableBehavior
isNewSlugNeeded() Checks whether the new slug generation is needed This method is called by getValue() to check whether the new slug generation is needed. yii\behaviors\SluggableBehavior
makeUnique() This method is called by getValue() when $ensureUnique is true to generate the unique slug. yii\behaviors\SluggableBehavior
validateSlug() Checks if given slug value is unique. yii\behaviors\SluggableBehavior

属性详情

$attribute 公共 属性

The attribute or list of attributes whose value will be converted into a slug or null meaning that the $value property will be used to generate a slug.

public string|array|null $attribute null
$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 boolean $ensureUnique false
$immutable 公共 属性 (自版本 2.0.2 可用)

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.

public boolean $immutable false
$skipOnEmpty 公共 属性 (自版本 2.0.13 可用)

Whether to skip slug generation if $attribute is null or an empty string. If true, the behaviour will not generate a new slug if $attribute is null or an empty string.

public boolean $skipOnEmpty false
$slugAttribute 公共 属性

The attribute that will receive the slug value

public string $slugAttribute 'slug'
$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:

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 yii\validators\UniqueValidator will be used.

参见 yii\validators\UniqueValidator.

public array $uniqueValidator = []
$value 公共 属性

The value that will be used as a slug. This can be an anonymous function or an arbitrary value or null. If the former, the return value of the function will be used as a slug. If null then the $attribute property will be used to generate a slug. The signature of the function should be as follows,

function ($event)
{
    // return slug
}
public callable|string|null $value null

方法详情

generateSlug() 受保护 方法

This method is called by getValue() to generate the slug.

You may override it to customize slug generation. The default implementation calls yii\helpers\Inflector::slug() on the input strings concatenated by dashes (-).

protected string generateSlug($slugParts)
$slugParts array

An array of strings that should be concatenated and converted to generate the slug value.

return string

The conversion result.

generateUniqueSlug() 受保护 方法

Generates slug using configured callback or increment of iteration.

protected string generateUniqueSlug($baseSlug, $iteration)
$baseSlug string

Base slug value

$iteration integer

Iteration number

return string

New slug value

throws yii\base\InvalidConfigException
getValue() 受保护 方法

返回给当前属性准备的值。 该方法在 evaluateAttributes() 里调用。 它的返回值将会设置到对应触发事件的属性上。

protected mixed getValue($event)
$event yii\base\Event

触发当前属性更新的事件

return mixed

属性值

init() 公共 方法

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init()
isEmpty() 受保护 方法 (自版本 2.0.13 可用)

Checks if $slugPart is empty string or null.

protected boolean isEmpty($slugPart)
$slugPart string

One of attributes that is used for slug generation.

return boolean

Whether $slugPart empty or not.

isNewSlugNeeded() 受保护 方法 (自版本 2.0.7 可用)

Checks whether the new slug generation is needed This method is called by getValue() to check whether the new slug generation is needed.

You may override it to customize checking.

protected boolean isNewSlugNeeded()
makeUnique() 受保护 方法 (自版本 2.0.7 可用)

This method is called by getValue() when $ensureUnique is true to generate the unique slug.

Calls generateUniqueSlug() until generated slug is unique and returns it.

参见:

protected string makeUnique($slug)
$slug string

Basic slug value

return string

Unique slug

validateSlug() 受保护 方法

Checks if given slug value is unique.

protected boolean validateSlug($slug)
$slug string

Slug value

return boolean

Whether slug is unique.