mintao/yii-behavior-sluggable

将一个或多个数据库字段转换为唯一的URL

这个包的官方仓库似乎已消失,因此该包已被冻结。

1.0 2013-08-05 21:02 UTC

This package is not auto-updated.

Last update: 2022-03-29 23:48:48 UTC


README

使用此行为,您可以生成表中的一列或多列的URI。有些人称它为永久链接,其他人称它为别名或可读URL。

查看最新版本: github.com/mintao/yii-behavior-sluggable

想象一个博客表

| id | category | title                             |
|----+----------+-----------------------------------|
|  1 | security | NASA Server hacked by hacker kids |
| ...

那么,您想要一个更好的URL吗?

http://your-blog.org/index.php?r=blog/show&id=1422

关于这个呢?

http://your-blog.org/security/nasa-server-hacked-by-hacker-kids

谷歌会喜欢您 ;)

如何完成它

  • 向您的表中添加一个名为 "slug" 的另一列
  • 下载此扩展并将其放入受保护的 "extensions" 文件夹中
  • 将此行为添加到您的模型中(如下所示)

如果您正在使用git,我建议

cd <YOUR YII-PROJECT>
mkdir -p protected/extensions/behaviors (optional)
git submodule add git://github.com/mintao/yii-behavior-sluggable.git protected/extensions/behaviors/SluggableBehavior

此行为为您模型的示例配置

/**
 * Behaviors for this model
 */
public function behaviors(){
  return array(
    'sluggable' => array(
      'class'=>'ext.behaviors.SluggableBehavior.SluggableBehavior',
      'columns' => array('category', 'title', 'author.name'),
      'unique' => true,
      'update' => true,
    ),
  );
}
  • class 定义找到 SluggableBehavor.php 的路径
  • columns 需要是一个用于生成别名的字段数组
  • unique 将此设置为 true 以确保别名唯一(如果已存在,则会在重复的别名中添加数字)
  • update 如果每次更改条目时都应该更新别名,则将此设置为 true。如果别名应该只创建一次,则将其设置为 false

下载此扩展(并对其进行分支)

github.com/mintao/yii-behavior-sluggable

变更日志

  • 2014-08-25 稍微提高速度
  • 2011-04-30 添加 update 功能
  • 2011-06-23 添加对依赖模型的支持(请参阅示例配置,author.name)