如何使用 Yii2 Sluggable Behavior?
How to use Yii2 Sluggable Behavior?
我已按照文档说明定义了此行为。
public function behaviors()
{
return [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
],
];
}
在我的配置 url 管理器中,我定义了这样的自定义规则:example.com/article/1
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'article/<id:\d+>/<slug>' => 'article/view',
],
],
我的观看操作是:
public function actionView($id, $slug = null)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
在我的索引视图文件中,我正在生成 URL 来查看这样的操作:Url::to(['article/view', 'id' => $model->id, 'slug' => $model->slug])
我想在 url 中输出我的文章标题,如下所示:example.com/article/1/My-first-post
但我没有在 URL 中获得称号。
Soju 说 slug 是数据库属性。我在我的文章 table 中创建了名为 slug 的新专栏,它是 varchar 1024。但我仍然没有在 URL 中生成 slug。我的 URL 是:example.com/article/1
怎么了?谢谢
编辑:我更新了我的代码,将标题值插入文章 table 的 slug 列中。现在我可以正常工作了,但是我没有 SEO URL-s。我明白了:article/1/First+Article
,我想要 article/1/First-Article
。
我试过:
return [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
'value' => function ($event) {
return str_replace(' ', '-', $this->slug);
}
],
];
这也不起作用:return str_replace(' ', '-', $this->slug);
您可以添加以下 urlManager
规则:
'article/<id:\d+>/<slug>' => 'article/view',
然后像这样在您的视图中构建 url :
\yii\helpers\Url::to(['article/view', 'id'=>$model->id, 'slug'=>$model->slug])
您还可以在模型中添加助手:
public function getRoute()
{
return ['article/view', 'id'=>$this->id, 'slug'=>$this->slug];
}
public function getUrl()
{
return \yii\helpers\Url::to($this->getRoute());
}
然后只需在您的视图中使用 $model->url
。
我已按照文档说明定义了此行为。
public function behaviors()
{
return [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
],
];
}
在我的配置 url 管理器中,我定义了这样的自定义规则:example.com/article/1
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'article/<id:\d+>/<slug>' => 'article/view',
],
],
我的观看操作是:
public function actionView($id, $slug = null)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
在我的索引视图文件中,我正在生成 URL 来查看这样的操作:Url::to(['article/view', 'id' => $model->id, 'slug' => $model->slug])
我想在 url 中输出我的文章标题,如下所示:example.com/article/1/My-first-post
但我没有在 URL 中获得称号。
Soju 说 slug 是数据库属性。我在我的文章 table 中创建了名为 slug 的新专栏,它是 varchar 1024。但我仍然没有在 URL 中生成 slug。我的 URL 是:example.com/article/1
怎么了?谢谢
编辑:我更新了我的代码,将标题值插入文章 table 的 slug 列中。现在我可以正常工作了,但是我没有 SEO URL-s。我明白了:article/1/First+Article
,我想要 article/1/First-Article
。
我试过:
return [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
'value' => function ($event) {
return str_replace(' ', '-', $this->slug);
}
],
];
这也不起作用:return str_replace(' ', '-', $this->slug);
您可以添加以下 urlManager
规则:
'article/<id:\d+>/<slug>' => 'article/view',
然后像这样在您的视图中构建 url :
\yii\helpers\Url::to(['article/view', 'id'=>$model->id, 'slug'=>$model->slug])
您还可以在模型中添加助手:
public function getRoute()
{
return ['article/view', 'id'=>$this->id, 'slug'=>$this->slug];
}
public function getUrl()
{
return \yii\helpers\Url::to($this->getRoute());
}
然后只需在您的视图中使用 $model->url
。