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', ], ]; }
Since: 2.0
Author: Alexander Kochetov (creocoder@gmail.com)
Author: Paul Klimov (klimov.paul@gmail.com)
Inheritance: extends AttributeBehavior
コード例 #1
0
ファイル: Slug.php プロジェクト: zelenin/yii2-slug-behavior
 /**
  * @inheritdoc
  */
 protected function getValue($event)
 {
     /* @var $owner ActiveRecord */
     $owner = $this->owner;
     if (!empty($owner->{$this->slugAttribute}) && !$this->slugIsEmpty && $this->immutable) {
         $slug = $owner->{$this->slugAttribute};
     } else {
         if ($owner->getIsNewRecord()) {
             $this->slugIsEmpty = true;
         }
         if ($this->attribute !== null) {
             $attributes = $this->attribute;
             $slugParts = array_map(function ($attribute) {
                 return ArrayHelper::getValue($this->owner, $attribute);
             }, $attributes);
             $slug = $this->slugify(implode($this->replacement, $slugParts), $this->replacement, $this->lowercase);
             if (!$owner->getIsNewRecord() && $this->slugIsEmpty) {
                 $owner->{$this->slugAttribute} = $slug;
                 $owner->save(false, [$this->slugAttribute]);
             }
         } else {
             $slug = parent::getValue($event);
         }
     }
     if ($this->ensureUnique) {
         $baseSlug = $slug;
         $iteration = 0;
         while (!$this->validateSlug($slug)) {
             $iteration++;
             $slug = $this->generateUniqueSlug($baseSlug, $iteration);
         }
     }
     return $slug;
 }
コード例 #2
0
ファイル: Municipios.php プロジェクト: alejandrososa/AB
 public function behaviors()
 {
     return [['class' => SluggableBehavior::className(), 'attribute' => 'nombre', 'immutable' => true, 'ensureUnique' => true], 'sitemap' => ['class' => SitemapBehavior::className(), 'scope' => function ($model) {
         /** @var \yii\db\ActiveQuery $model */
         $model->select(['municipios.slug']);
     }, 'dataClosure' => function ($model) {
         /** @var self $model */
         return ['loc' => Url::to($model->slug, true), 'changefreq' => SitemapBehavior::CHANGEFREQ_NEVER, 'priority' => 0.5];
     }]];
 }
コード例 #3
0
ファイル: Category.php プロジェクト: manyoubaby123/imshop
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return ['timestamp' => TimestampBehavior::className(), 'sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'name'], 'tree' => ['class' => NestedSetsBehavior::className(), 'treeAttribute' => 'tree'], 'files' => ['class' => FilesBehavior::className(), 'attributes' => ['uploadedImage' => ['filesystem' => 'local', 'path' => '/categories', 'fileName' => '{model.slug}.{file.extension}', 'relation' => 'image', 'deleteOnUnlink' => true, 'on beforeSave' => function (FileInterface $file) {
         $image = ImageManagerStatic::make($file->getPath());
         $image->resize(400, null, function (Constraint $constraint) {
             $constraint->aspectRatio();
             $constraint->upsize();
         });
         $image->save($file->getPath(), 100);
     }]]], 'relations' => ['class' => RelationsBehavior::className(), 'settings' => ['image' => ['deleteOnUnlink' => true]], 'relations' => ['imageRelation' => $this->hasOne(CategoryFile::className(), ['id' => 'image_id'])]]];
 }
コード例 #4
0
ファイル: Page.php プロジェクト: hassiumsoft/hasscms-packages
 public function behaviors()
 {
     $behaviors = ['sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'ensureUnique' => true, "immutable" => true], SetMaxSortableModel::className()];
     $behaviors['timestamp'] = TimestampBehavior::className();
     $behaviors['published_at'] = ['class' => \hass\base\behaviors\StrToTimeBehavior::className(), "attribute" => "published_at"];
     $behaviors["meta"] = MetaBehavior::className();
     $behaviors["thumbnailFile"] = ['class' => \hass\attachment\behaviors\UploadBehavior::className(), 'attribute' => 'thumbnail'];
     $behaviors["textEditor"] = ['class' => \hass\base\misc\editor\EditorBehavior::className(), 'attribute' => 'content'];
     $behaviors["TimestampFormatter"] = TimestampFormatter::className();
     $behaviors["commentEnabled"] = ['class' => CommentBehavior::className(), 'defaultStatus' => CommentEnabledEnum::STATUS_ON];
     return $behaviors;
 }
コード例 #5
0
ファイル: Product.php プロジェクト: xyxdasnjss/imshop
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return ['sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'ensureUnique' => true], 'timestamp' => TimestampBehavior::className(), 'files' => ['class' => FilesBehavior::className(), 'attributes' => ['uploadedImages' => ['filesystem' => 'local', 'path' => '/products/{model.id}', 'fileName' => '{model.slug}.{file.extension}', 'multiple' => true, 'relation' => 'images', 'on beforeSave' => function (FileInterface $file) {
         $image = ImageManagerStatic::make($file->getPath());
         $image->resize(1000, null, function (Constraint $constraint) {
             $constraint->aspectRatio();
             $constraint->upsize();
         });
         $image->save($file->getPath(), 100);
     }, 'on afterDeleteAll' => function (StorageConfig $config, FilesystemComponent $filesystemComponent) {
         $path = $config->resolvePath($this);
         $filesystemComponent->deleteDirectory($path, $config->filesystem);
     }]]], 'relations' => ['class' => RelationsBehavior::className(), 'settings' => ['relatedEAttributes' => ['deleteOnUnlink' => true], 'images' => ['deleteOnUnlink' => true, 'extraColumns' => ['attribute' => 'images']]], 'relations' => ['imagesRelation' => $this->hasMany(ProductFile::className(), ['product_id' => 'id'])->where(['attribute' => 'images'])]]];
 }
コード例 #6
0
ファイル: Post.php プロジェクト: rocketyang/hasscms-app
 public function behaviors()
 {
     $behaviors = ['sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'ensureUnique' => true, "immutable" => true]];
     $behaviors['author_id'] = ['class' => BlameableBehavior::className(), 'attributes' => [static::EVENT_BEFORE_INSERT => 'author_id']];
     $behaviors['timestamp'] = TimestampBehavior::className();
     $behaviors['published_at'] = ['class' => StrToTimeBehavior::className(), 'attribute' => 'published_at'];
     $behaviors['taggabble'] = Taggable::className();
     $behaviors['meta'] = MetaBehavior::className();
     $behaviors['thumbnailFile'] = ['class' => \hass\attachment\behaviors\UploadBehavior::className(), 'attribute' => 'thumbnail'];
     $behaviors['taxonomy'] = ['class' => TaxonomyBehavior::className()];
     $behaviors['textEditor'] = ['class' => \hass\base\misc\editor\EditorBehavior::className(), 'attribute' => 'content'];
     $behaviors['TimestampFormatter'] = TimestampFormatter::className();
     $behaviors['commentEnabled'] = ['class' => CommentBehavior::className(), 'defaultStatus' => CommentEnabledEnum::STATUS_ON];
     return $behaviors;
 }
コード例 #7
0
ファイル: SluggableBehavior.php プロジェクト: ahb360/kalpok
 public function getValue($event)
 {
     $this->setLanguage();
     if ($this->language != 'fa') {
         return parent::getValue($event);
     }
     $isNewSlug = true;
     if ($this->attribute !== null) {
         $attributes = (array) $this->attribute;
         /* @var $owner BaseActiveRecord */
         $owner = $this->owner;
         if (!empty($owner->{$this->slugAttribute})) {
             $isNewSlug = false;
             if (!$this->immutable) {
                 foreach ($attributes as $attribute) {
                     if ($owner->isAttributeChanged($attribute)) {
                         $isNewSlug = true;
                         break;
                     }
                 }
             }
         }
         if ($isNewSlug) {
             $slugParts = [];
             foreach ($attributes as $attribute) {
                 $slugParts[] = $owner->{$attribute};
             }
             $slug = Inflector::persianSlug(implode('-', $slugParts));
         } else {
             $slug = $owner->{$this->slugAttribute};
         }
     } else {
         $slug = parent::getValue($event);
     }
     if ($this->ensureUnique && $isNewSlug) {
         $baseSlug = $slug;
         $iteration = 0;
         while (!$this->validateSlug($slug)) {
             $iteration++;
             $slug = $this->generateUniqueSlug($baseSlug, $iteration);
         }
     }
     return $slug;
 }
コード例 #8
0
ファイル: SluggableBehavior.php プロジェクト: akula22/fifa
 /**
  * @inheritdoc
  */
 protected function getValue($event)
 {
     $isNewSlug = true;
     if ($this->attribute !== null) {
         $attributes = (array) $this->attribute;
         /* @var $owner BaseActiveRecord */
         $owner = $this->owner;
         if (!$owner->getIsNewRecord() && !empty($owner->{$this->slugAttribute})) {
             $isNewSlug = false;
             foreach ($attributes as $attribute) {
                 if ($owner->isAttributeChanged($attribute) && $this->forceUpdate) {
                     $isNewSlug = true;
                     break;
                 }
             }
         }
         if ($isNewSlug) {
             $slugParts = [];
             foreach ($attributes as $attribute) {
                 $slugParts[] = $owner->{$attribute};
             }
             $oldTransliterator = Inflector::$transliterator;
             if (isset($this->transliterator)) {
                 Inflector::$transliterator = $this->transliterator;
             }
             $slug = Inflector::slug(implode('_', $slugParts));
             Inflector::$transliterator = $oldTransliterator;
         } else {
             $slug = $owner->{$this->slugAttribute};
         }
     } else {
         $slug = parent::getValue($event);
     }
     if ($this->ensureUnique && $isNewSlug) {
         $baseSlug = $slug;
         $iteration = 0;
         while (!$this->validateSlug($slug)) {
             $iteration++;
             $slug = $this->generateUniqueSlug($baseSlug, $iteration);
         }
     }
     return $slug;
 }
コード例 #9
0
ファイル: Album.php プロジェクト: yeesoft/yii2-yee-media
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [BlameableBehavior::className(), TimestampBehavior::className(), 'sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title'], 'multilingual' => ['class' => MultilingualBehavior::className(), 'langForeignKey' => 'media_album_id', 'tableName' => "{{%media_album_lang}}", 'attributes' => ['title', 'description']]];
 }
コード例 #10
0
ファイル: Page.php プロジェクト: promo-pr/cms
 public function behaviors()
 {
     return [['class' => TimestampBehavior::className()], ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'immutable' => true, 'ensureUnique' => true], ['class' => FileBehavior::className(), 'attribute' => 'image']];
 }
コード例 #11
0
ファイル: Finance.php プロジェクト: allhaze/renault
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [TimestampBehavior::className(), ['class' => BlameableBehavior::className(), 'createdByAttribute' => 'author_id', 'updatedByAttribute' => 'updater_id'], ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'immutable' => true], ['class' => UploadBehavior::className(), 'attribute' => 'attachments', 'multiple' => true, 'uploadRelation' => 'financeAttachments'], ['class' => UploadBehavior::className(), 'attribute' => 'thumbnail', 'pathAttribute' => 'thumbnail_path', 'baseUrlAttribute' => 'thumbnail_base_url'], ['class' => ChangeLogBehavior::className()]];
 }
コード例 #12
0
 public function behaviors()
 {
     return [['class' => SluggableBehavior::className(), 'attribute' => 'name'], 'timestamp' => ['class' => TimestampBehavior::className()], ['class' => BlameableBehavior::className()]];
 }
コード例 #13
0
ファイル: Role.php プロジェクト: cmsgears/module-core
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return ['authorBehavior' => ['class' => AuthorBehavior::className()], 'sluggableBehavior' => ['class' => SluggableBehavior::className(), 'attribute' => 'name', 'slugAttribute' => 'slug', 'ensureUnique' => true], 'timestampBehavior' => ['class' => TimestampBehavior::className(), 'createdAtAttribute' => 'createdAt', 'updatedAtAttribute' => 'modifiedAt', 'value' => new Expression('NOW()')]];
 }
コード例 #14
0
ファイル: Noticia.php プロジェクト: nando9/yii2capa8
 public function behaviors()
 {
     return ['timestamp' => ['class' => 'yii\\behaviors\\TimestampBehavior', 'attributes' => [ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']], 'value' => new Expression('NOW()')], 'blameable' => ['class' => \yii\behaviors\BlameableBehavior::className(), 'createdByAttribute' => 'created_by', 'updatedByAttribute' => 'updated_by'], ['class' => SluggableBehavior::className(), 'attribute' => 'titulo', 'slugAttribute' => 'seo_slug']];
 }
コード例 #15
0
ファイル: Template.php プロジェクト: ramialcheikh/quickforms
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [BlameableBehavior::className(), TimestampBehavior::className(), ['class' => SluggableBehavior::className(), 'attribute' => 'name', 'ensureUnique' => true], 'builderField' => ['class' => JsonToArrayBehavior::className(), 'jsonFieldName' => 'builder']];
 }
コード例 #16
0
ファイル: Menu.php プロジェクト: bitxseven/yii2-big
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return ['tree' => ['class' => NestedSetsBehavior::className(), 'treeAttribute' => 'tree'], ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'slugAttribute' => 'alias', 'immutable' => true]];
 }
コード例 #17
0
ファイル: Post.php プロジェクト: pramana08/app-cms
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [['class' => SluggableBehavior::className(), 'attribute' => 'post_title', 'attributes' => [ActiveRecord::EVENT_BEFORE_INSERT => ['post_slug']]]];
 }
コード例 #18
0
ファイル: Page.php プロジェクト: nguyentuansieu/phutungoto
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [['class' => SluggableBehavior::className(), 'attribute' => 'slug', 'slugAttribute' => 'slug', 'ensureUnique' => true], ['class' => TimestampBehavior::className()], ['class' => BlameableBehavior::className()]];
 }
コード例 #19
0
ファイル: CategoryModel.php プロジェクト: tqsq2005/Yii2adv
 public function behaviors()
 {
     return ['cacheflush' => ['class' => CacheFlush::className(), 'key' => [static::tableName() . '_tree', static::tableName() . '_flat']], 'sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'ensureUnique' => true], 'tree' => ['class' => NestedSetsBehavior::className(), 'treeAttribute' => 'tree'], 'timestamp' => ['class' => TimestampBehavior::className()], 'blame' => ['class' => BlameableBehavior::className()], 'audit' => ['class' => ARLogBehavior::className()]];
 }
コード例 #20
0
ファイル: Item.php プロジェクト: radiegtya/easyii
 public function behaviors()
 {
     return [SortableModel::className(), 'seo' => SeoBehavior::className(), 'sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'ensureUnique' => true], 'blameable' => ['class' => BlameableBehavior::className(), 'createdByAttribute' => 'created_by', 'updatedByAttribute' => 'updated_by']];
 }
コード例 #21
0
ファイル: Common.php プロジェクト: janisto/yii2-ycm-demo
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [['class' => SluggableBehavior::className(), 'attribute' => 'title', 'immutable' => true, 'ensureUnique' => true], ['class' => TimestampBehavior::className(), 'value' => new Expression('UTC_TIMESTAMP()')], ['class' => FileBehavior::className(), 'folderName' => 'common']];
 }
コード例 #22
0
 public function behaviors()
 {
     return [TimestampBehavior::className(), ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'immutable' => true]];
 }
コード例 #23
0
ファイル: News.php プロジェクト: griga/m22-cms
 public function behaviors()
 {
     return array_merge(parent::behaviors(), ['sluggable' => ['class' => \yii\behaviors\SluggableBehavior::className(), 'slugAttribute' => 'alias', 'attribute' => 'title'], 'linkable' => ['class' => \app\components\models\LinkableBehavior::className(), 'urlPath' => '/' . \Yii::$app->lang->get() . '/news/']]);
 }
コード例 #24
0
ファイル: Page.php プロジェクト: bigbrush/yii2-cmf
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [TimestampBehavior::className(), BlameableBehavior::className(), ['class' => SluggableBehavior::className(), 'attribute' => ['title', 'alias'], 'slugAttribute' => 'alias', 'ensureUnique' => true], ['class' => EditorBehavior::className(), 'introAttribute' => 'intro_content', 'contentAttribute' => 'content', 'purifyContent' => true]];
 }
コード例 #25
0
ファイル: CategoryModel.php プロジェクト: solbianca/easyii
 public function behaviors()
 {
     return ['cacheflush' => ['class' => CacheFlush::className(), 'key' => [static::tableName() . '_tree', static::tableName() . '_flat']], 'seoBehavior' => SeoBehavior::className(), 'sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'ensureUnique' => true], 'tree' => ['class' => NestedSetsBehavior::className(), 'treeAttribute' => 'tree']];
 }
コード例 #26
0
ファイル: File.php プロジェクト: Brother-Simon/easyii
 public function behaviors()
 {
     return [SortableModel::className(), 'seoBehavior' => SeoBehavior::className(), 'sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'ensureUnique' => true]];
 }
コード例 #27
0
ファイル: Content.php プロジェクト: Mitonios/mitonios-blog
 public function behaviors()
 {
     return [['class' => SluggableBehavior::className(), 'attribute' => 'title'], TimestampBehavior::className()];
 }
コード例 #28
0
ファイル: Article.php プロジェクト: gangbo/yii2-starter-kit
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [TimestampBehavior::className(), ['class' => BlameableBehavior::className(), 'createdByAttribute' => 'author_id', 'updatedByAttribute' => 'updater_id'], ['class' => SluggableBehavior::className(), 'attribute' => 'title', 'immutable' => true], ['class' => UploadBehavior::className(), 'attribute' => 'attachments', 'multiple' => true, 'uploadRelation' => 'articleAttachments', 'pathAttribute' => 'path', 'baseUrlAttribute' => 'base_url', 'orderAttribute' => 'order', 'typeAttribute' => 'type', 'sizeAttribute' => 'size', 'nameAttribute' => 'name'], ['class' => UploadBehavior::className(), 'attribute' => 'thumbnail', 'pathAttribute' => 'thumbnail_path', 'baseUrlAttribute' => 'thumbnail_base_url']];
 }
コード例 #29
0
ファイル: Tag.php プロジェクト: beaten-sect0r/yii2-core
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [['class' => SluggableBehavior::className(), 'attribute' => 'name', 'ensureUnique' => true, 'immutable' => true]];
 }
コード例 #30
0
ファイル: Category.php プロジェクト: yeesoft/yii2-yee-post
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [BlameableBehavior::className(), TimestampBehavior::className(), 'sluggable' => ['class' => SluggableBehavior::className(), 'attribute' => 'title'], 'multilingual' => ['class' => MultilingualBehavior::className(), 'langForeignKey' => 'post_category_id', 'tableName' => "{{%post_category_lang}}", 'attributes' => ['title', 'description']], 'nestedInterval' => ['class' => NestedIntervalsBehavior::className(), 'leftAttribute' => 'left_border', 'rightAttribute' => 'right_border', 'amountOptimize' => '25', 'noPrepend' => true]];
 }