W3cubDocs

/Yii 2.0

Class yii\behaviors\SluggableBehavior

Inheritance yii\behaviors\SluggableBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\Object
Implements yii\base\Configurable
Available since version 2.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.

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',
        ],
    ];
}

Public Properties

Property Type Description Defined By
$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 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|null The owner of this behavior yii\base\Behavior
$preserveNonEmptyValues boolean Whether to preserve non-empty attribute values. yii\behaviors\AttributeBehavior
$skipUpdateOnClean boolean Whether to skip this behavior when the $owner has not been modified 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

Public Methods

Method Description Defined 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

Method Description Defined By
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() Returns the value for the current attributes. 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

Property Details

$attribute public property

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 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.

public boolean $ensureUnique = false

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

public boolean $immutable = false

$slugAttribute public property

The attribute that will receive the slug value

public string $slugAttribute = 'slug'

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

public callable $uniqueSlugGenerator = null

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

public array $uniqueValidator = []

$value public property

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

Method Details

generateSlug() protected method

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() protected method

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() protected method

Returns the value for the current attributes.

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

protected 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.

public void init ( )

isNewSlugNeeded() protected method (available since version 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() protected method (available since version 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.

See also:

protected string makeUnique ( $slug )
$slug string

Basic slug value

return string

Unique slug

validateSlug() protected method

Checks if given slug value is unique.

protected boolean validateSlug ( $slug )
$slug string

Slug value

return boolean

Whether slug is unique.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-behaviors-sluggablebehavior.html