Skip to content
PHP Ecosystem
Explore

SluggableBehavior

API reference › yii › behaviors › SluggableBehavior

classyii\behaviors\SluggableBehavior
ExtendsAttributeBehavior » yii\base\Behavior » yii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/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 Inflector::$transliteration.

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

use yii\behaviors\SluggableBehavior;
public function behaviors()
{
return [
[
'class' => SluggableBehavior::class,
'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::class,
'slugAttribute' => 'alias',
],
];
}
PropertyTypeDescriptionDefined by
$attributestring|array|nullThe 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. Default: null.SluggableBehavior
$attributesarrayList of attributes that are to be automatically filled with the value specified via $value. Default: [].AttributeBehavior
$ensureUniqueboolWhether to ensure generated slug value to be unique among owner class records. Default: false.SluggableBehavior
$immutableboolWhether to generate a new slug if it has already been generated before. Default: false. Available since 2.0.2.SluggableBehavior
$owneryii\base\T|nullThe owner of this behavior Default: null.yii\base\Behavior
$preserveNonEmptyValuesboolWhether to preserve non-empty attribute values. Default: false. Available since 2.0.13.AttributeBehavior
$skipOnEmptyboolWhether to skip slug generation if $attribute is null or an empty string. Default: false. Available since 2.0.13.SluggableBehavior
$skipUpdateOnCleanboolWhether to skip this behavior when the $owner has not been modified Default: true. Available since 2.0.8.AttributeBehavior
$slugAttributestringThe attribute that will receive the slug value Default: 'slug'.SluggableBehavior
$uniqueSlugGeneratorcallable|nullSlug unique value generator. Default: null.SluggableBehavior
$uniqueValidatorarrayConfiguration for slug uniqueness validator. Default: [].SluggableBehavior
$valuecallable|string|nullThe value that will be used as a slug. Default: null.SluggableBehavior
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.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() staticReturns the fully qualified name of this class.yii\base\BaseObject
detach()Detaches the behavior object from the component.yii\base\Behavior
evaluateAttributes()Evaluates the attribute value and assigns it to the current attributes.AttributeBehavior
events()Declares event handlers for the $owner’s events.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.SluggableBehavior
generateSlug() protectedThis method is called by getValue() to generate the slug.SluggableBehavior
generateUniqueSlug() protectedGenerates slug using configured callback or increment of iteration.SluggableBehavior
getValue() protectedReturns the value for the current attributes.SluggableBehavior
isEmpty() protectedChecks if $slugPart is empty string or null.SluggableBehavior
isNewSlugNeeded() protectedChecks whether the new slug generation is needed This method is called by getValue() to check whether the new slug generation is needed.SluggableBehavior
makeUnique() protectedThis method is called by getValue() when $ensureUnique is true to generate the unique slug.SluggableBehavior
validateSlug() protectedChecks if given slug value is unique.SluggableBehavior
public bool $ensureUnique = false

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 bool $immutable = false

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 bool $skipOnEmpty = false

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.

Since

2.0.13

public callable|null $uniqueSlugGenerator = null

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 array $uniqueValidator = []

Configuration for slug uniqueness validator.

Parameter ‘class’ may be omitted - by default UniqueValidator will be used.

public callable|string|null $value = null

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 function init(): mixed

Initializes the object.

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

Returns

mixed

protected function generateSlug(array $slugParts): string

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

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

Parameters

ParameterTypeDescription
$slugPartsarrayAn array of strings that should be concatenated and converted to generate the slug value.

Returns

string: The conversion result.

protected function generateUniqueSlug(string $baseSlug, int $iteration): string

Generates slug using configured callback or increment of iteration.

Parameters

ParameterTypeDescription
$baseSlugstringBase slug value
$iterationintIteration number

Returns

string: New slug value

Throws

protected function getValue(mixed $event): mixed

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.

Parameters

ParameterTypeDescription
$eventmixedThe event that triggers the current attribute updating.

Returns

mixed: The attribute value

protected function isEmpty(string $slugPart): bool

Checks if $slugPart is empty string or null.

Parameters

ParameterTypeDescription
$slugPartstringOne of attributes that is used for slug generation.

Returns

bool: Whether $slugPart empty or not.

Since

2.0.13

protected function isNewSlugNeeded(): bool

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.

Returns

bool

Since

2.0.7

protected function makeUnique(string $slug): string

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.

Parameters

ParameterTypeDescription
$slugstringBasic slug value

Returns

string: Unique slug

Since

2.0.7

See also

protected function validateSlug(string $slug): bool

Checks if given slug value is unique.

Parameters

ParameterTypeDescription
$slugstringSlug value

Returns

bool: Whether slug is unique.