1

I have defined this behavior as per documentation instructions.

public function behaviors()
{
    return [
        TimestampBehavior::className(),
        [
            'class' => SluggableBehavior::className(),
            'attribute' => 'title',
        ],
    ];
}

In my config url manager I have defined custom rule like this: example.com/article/1

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        'article/<id:\d+>/<slug>' => 'article/view',
    ],
],

My view action is:

public function actionView($id, $slug = null)
{
    return $this->render('view', [
        'model' => $this->findModel($id),
    ]);
}

In my index view file I am generating URL to view action like this : Url::to(['article/view', 'id' => $model->id, 'slug' => $model->slug])

I would like to output my article title in url like this: example.com/article/1/My-first-post

But I am not getting title in URL.

Soju said that slug is a database attribute. I have created new column in my article table called slug and it is varchar 1024. But I am still not getting slug generated in URL. My URL is: example.com/article/1

What is wrong ? Thanks

EDIT: I have updated my code to insert title value into slug column in my article table. Now I get slug working but I do not get SEO URL-s. I get this: article/1/First+Article, and I would like article/1/First-Article.

I have tried with:

return [
    TimestampBehavior::className(),
    [
        'class' => SluggableBehavior::className(),
        'attribute' => 'title',
        'value' => function ($event) {
            return str_replace(' ', '-', $this->slug);
        }
    ],
];

This doesn't work either: return str_replace(' ', '-', $this->slug);

Anita
  • 159
  • 2
  • 11

1 Answers1

4

You could add the following urlManager rule :

'article/<id:\d+>/<slug>' => 'article/view',

And build url in your views like this :

\yii\helpers\Url::to(['article/view', 'id'=>$model->id, 'slug'=>$model->slug])

You could also add helpers in your model :

public function getRoute()
{
    return ['article/view', 'id'=>$this->id, 'slug'=>$this->slug];
}

public function getUrl()
{
    return \yii\helpers\Url::to($this->getRoute());
}

And then simply use $model->url in your views.

soju
  • 25,111
  • 3
  • 68
  • 70
  • If I do it like that, I will get this url: `article/view?id=1`, slug is not used. – Anita Mar 03 '15 at 22:39
  • Of course I did, and it is not working. Have you configured sluggable behavior like I did in post ? – Anita Mar 04 '15 at 07:00
  • 1
    "it is not working" is not enough, please add details : error message or else – soju Mar 04 '15 at 07:01
  • I already told you that url is article/view?id=1, it does not have slug in it. There are no error messages. URL does not output slug – Anita Mar 04 '15 at 09:20
  • `Url::to(['article/view', 'id' => $model->id, 'slug' => $model->slug])` – Anita Mar 04 '15 at 09:39
  • Sluggable Behaviour is defined in my Article Model like this: `public function behaviors() { return [ TimestampBehavior::className(), [ 'class' => SluggableBehavior::className(), 'attribute' => 'title', 'value' => function ($event) { return str_replace(' ', '-', $this->title); } ], ]; }` – Anita Mar 04 '15 at 09:40
  • actionVeiw is : `public function actionView($id, $slug = null) { return $this->render('view', [ 'model' => $this->findModel($id), ]); }` – Anita Mar 04 '15 at 09:42
  • And in Article model, there is slug attribute defined: `public $slug;` – Anita Mar 04 '15 at 09:43
  • Please update your question instead of adding comments... And slug is a database attribute, it should not be declared like this. – soju Mar 04 '15 at 10:02
  • Did you remove `public $slug` in your article model ? – soju Mar 04 '15 at 10:57
  • Now I did, and I get this url generated: `article/1`, and when I click on link to go to view page I get this error: `Page not found. ` – Anita Mar 04 '15 at 11:27
  • What do you mean ? Update what, where ? When I create some article, I need to copy title to slug ? Right ? – Anita Mar 04 '15 at 12:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72227/discussion-between-soju-and-anita). – soju Mar 04 '15 at 12:54