Class yii\behaviors\SluggableBehavior

Inheritanceyii\behaviors\SluggableBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\Object
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/behaviors/SluggableBehavior.php

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

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. If your attribute name is different, you may configure the $slugAttribute property like the following:

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

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$attribute string|array The attribute or list of attributes whose value will be converted into a slug yii\behaviors\SluggableBehavior
$attributes array List of attributes that are to be automatically filled with the value specified via $value. 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 The owner of this behavior yii\base\Behavior
$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 string|callable The value that will be used as a slug. yii\behaviors\SluggableBehavior

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Object
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
attach() Attaches the behavior object to the component. yii\base\Behavior
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
detach() Detaches the behavior object from the component. yii\base\Behavior
evaluateAttributes() Evaluates the attribute value and assigns it to the current attributes. 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\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the object. yii\behaviors\SluggableBehavior

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getValue() Returns the value of the current attributes. yii\behaviors\SluggableBehavior

Property Details

$attribute public property

The attribute or list of attributes whose value will be converted into a slug

$ensureUnique public 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 property (available since version 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.

$slugAttribute public property

The attribute that will receive the slug value

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

function ($baseSlug$iteration$model)
{
    
// return uniqueSlug
}

If not set unique slug will be generated adding incrementing suffix to the base slug.

$uniqueValidator public property

Configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default yii\validators\UniqueValidator will be used.

See also yii\validators\UniqueValidator.

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

function ($event)
{
    
// return slug
}

Method Details

getValue() protected method

Returns the value of the current attributes.

This method is called by evaluateAttributes(). Its return value will be assigned to the attributes corresponding to the triggering event.

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

The event that triggers the current attribute updating.

return mixed

The attribute value

init() public method

Initializes the object.

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

void init( )