PHP 클래스 yii\behaviors\SluggableBehavior
To use SluggableBehavior, insert the following code to your ActiveRecord class:
php
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 [[\yii\base\Model::rules()|rules()]] method of the model.
If your attribute name is different, you may configure the [[slugAttribute]] property like the following:
php
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'slugAttribute' => 'alias',
],
];
}
파일 보기
프로젝트 열기: yiisoft/yii2
1 사용 예제들
공개 프로퍼티들
프로퍼티 |
타입 |
설명 |
|
$attribute |
|
the attribute or list of attributes whose value will be converted into a slug |
|
$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. |
|
$immutable |
|
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 |
|
the attribute that will receive the slug value |
|
$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:
php
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
[[UniqueValidator]] will be used. |
|
$value |
|
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,
php
function ($event)
{
return 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 generateSlug ( array $slugParts ) : string |
$slugParts |
array |
an array of strings that should be concatenated and converted to generate the slug value. |
리턴 |
string |
the conversion result. |
generateUniqueSlug()
보호된 메소드
Generates slug using configured callback or increment of iteration.
isNewSlugNeeded()
보호된 메소드
You may override it to customize checking.
Calls [[generateUniqueSlug]] until generated slug is unique and returns it.
Checks if given slug value is unique.
프로퍼티 상세
the attribute or list of attributes whose value will be converted into a slug
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.
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 공개적으로 프로퍼티
the attribute that will receive the slug value
$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:
php
function ($baseSlug, $iteration, $model)
{
return uniqueSlug
}
If not set unique slug will be generated adding incrementing suffix to the base slug.
public $uniqueSlugGenerator |
$uniqueValidator 공개적으로 프로퍼티
configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default
[[UniqueValidator]] will be used.
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,
php
function ($event)
{
return slug
}