SluggableBehavior
API reference › yii › behaviors › SluggableBehavior
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', ], ];}Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$attribute | string| | 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. Default: null. | SluggableBehavior |
$attributes | array | List of attributes that are to be automatically filled with the value specified via $value. Default: []. | AttributeBehavior |
$ensureUnique | bool | Whether to ensure generated slug value to be unique among owner class records. Default: false. | SluggableBehavior |
$immutable | bool | Whether to generate a new slug if it has already been generated before. Default: false. Available since 2.0.2. | SluggableBehavior |
$owner | yii\ | The owner of this behavior Default: null. | yii\ |
$preserveNonEmptyValues | bool | Whether to preserve non-empty attribute values. Default: false. Available since 2.0.13. | AttributeBehavior |
$skipOnEmpty | bool | Whether to skip slug generation if $attribute is null or an empty string. Default: false. Available since 2.0.13. | SluggableBehavior |
$skipUpdateOnClean | bool | Whether to skip this behavior when the $owner has not been modified Default: true. Available since 2.0.8. | AttributeBehavior |
$slugAttribute | string | The attribute that will receive the slug value Default: 'slug'. | SluggableBehavior |
$uniqueSlugGenerator | callable| | Slug unique value generator. Default: null. | SluggableBehavior |
$uniqueValidator | array | Configuration for slug uniqueness validator. Default: []. | SluggableBehavior |
$value | callable| | The value that will be used as a slug. Default: null. | SluggableBehavior |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
attach() | Attaches the behavior object to the component. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detach() | Detaches the behavior object from the component. | yii\ |
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\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | SluggableBehavior |
generateSlug() protected | This method is called by getValue() to generate the slug. | SluggableBehavior |
generateUniqueSlug() protected | Generates slug using configured callback or increment of iteration. | SluggableBehavior |
getValue() protected | Returns the value for the current attributes. | SluggableBehavior |
isEmpty() protected | Checks if $slugPart is empty string or null. | SluggableBehavior |
isNewSlugNeeded() protected | Checks whether the new slug generation is needed This method is called by getValue() to check whether the new slug generation is needed. | SluggableBehavior |
makeUnique() protected | This method is called by getValue() when $ensureUnique is true to generate the unique slug. | SluggableBehavior |
validateSlug() protected | Checks if given slug value is unique. | SluggableBehavior |
Property details
Section titled “Property details”$ensureUnique
Section titled “$ensureUnique ”public bool $ensureUnique = falseWhether 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
Section titled “$immutable ”public bool $immutable = falseWhether 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
$skipOnEmpty
Section titled “$skipOnEmpty ”public bool $skipOnEmpty = falseWhether 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
$uniqueSlugGenerator
Section titled “$uniqueSlugGenerator ”public callable|null $uniqueSlugGenerator = nullSlug 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
Section titled “$uniqueValidator ”public array $uniqueValidator = []Configuration for slug uniqueness validator.
Parameter ‘class’ may be omitted - by default
UniqueValidator will be used.
$value
Section titled “$value ”public callable|string|null $value = nullThe 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}Method details
Section titled “Method details”init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed
generateSlug()
Section titled “generateSlug() ”protected function generateSlug(array $slugParts): stringThis 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
| Parameter | Type | Description |
|---|---|---|
$slugParts | array | An array of strings that should be concatenated and converted to generate the slug value. |
Returns
string: The conversion result.
generateUniqueSlug()
Section titled “generateUniqueSlug() ”protected function generateUniqueSlug(string $baseSlug, int $iteration): stringGenerates slug using configured callback or increment of iteration.
Parameters
| Parameter | Type | Description |
|---|---|---|
$baseSlug | string | Base slug value |
$iteration | int | Iteration number |
Returns
string: New slug value
Throws
getValue()
Section titled “getValue() ”protected function getValue(mixed $event): mixedReturns 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
| Parameter | Type | Description |
|---|---|---|
$event | mixed | The event that triggers the current attribute updating. |
Returns
mixed: The attribute value
isEmpty()
Section titled “isEmpty() ”protected function isEmpty(string $slugPart): boolChecks if $slugPart is empty string or null.
Parameters
| Parameter | Type | Description |
|---|---|---|
$slugPart | string | One of attributes that is used for slug generation. |
Returns
bool: Whether $slugPart empty or not.
Since
2.0.13
isNewSlugNeeded()
Section titled “isNewSlugNeeded() ”protected function isNewSlugNeeded(): boolChecks 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
makeUnique()
Section titled “makeUnique() ”protected function makeUnique(string $slug): stringThis 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
| Parameter | Type | Description |
|---|---|---|
$slug | string | Basic slug value |
Returns
string: Unique slug
Since
2.0.7
See also
validateSlug()
Section titled “validateSlug() ”protected function validateSlug(string $slug): boolChecks if given slug value is unique.
Parameters
| Parameter | Type | Description |
|---|---|---|
$slug | string | Slug value |
Returns
bool: Whether slug is unique.