Using SluggableBehavior to make nice URLs

have added SluggableBehavior as Docs say to my model and now I want URL to include a slug so I do something like this in view


Html::a(Html::encode($model->title), ['view', 'id' => $model->id, 'slug' => $model->slug])

But URL is the same something like

example.com/post/view?id=3

I want something like

example.com/post/view/3-some-nice-article-here

Anything extra I need to do?

Did you configured the UrlManager ?

Try something like that :




'rules' => [

  'post/view/<id:\d+>-<slug:\d+>' => 'post/view'

]



http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#url-rules

It does not work either. Why is this necessary anyway?

Try this

Rules




'<controller:\w+>/<id:\d+>-<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',



View




echo Html::a("View", ['view', 'id' => 4, 'slug' => "SLUG-VALUE"]);






http://127.0.0.1/yii2/advanced/backend/web/index.php/validation/4-SLUG-VALUE



May be I have misunderstood the whole usage of the behaviour. So I will post again with more details.

I have in model Blog the following code


public function behaviors()

	{

    	return [

        	[

            	'class' => SluggableBehavior::className(),

            	'attribute' => 'title',

            	// In case of attribute that contains slug has different name

            	// 'slugAttribute' => 'alias',

            	/*'value' => function ($event) {

                	return str_replace(' ', '-', $this->blog->name);

            	}*/

        	],

    	];

	}

I supposed it would add the attribute slug to model, it didn’t. So I defined a public attribute $slug and added to safe in model rules.

Still when I use with above code on the view, slug is empty. What am I doing wrong here?

Do you have a column "slug" in the table ?

It should save the slug in it and the attribute "title" should not be slugged.


public function behaviors()

{

  return [

    [

      'class' => SluggableBehavior::className(),

      'attribute' => 'title',

      'slugAttribute' => 'slug'

      ],

  ];

}

Actually I don’t want to have fixed slug database column. I want it to be generated on the fly!

Is that not what behavior is for?

I think it should work when you have set a public variable with the name slug. But I am not sure it is called on loading a model. Perhaps it only is called on a save?

If you don’t want to save it to the database, what do you want to do with it?

Only for generating URL that are meaningful. URL have an ID which I use to know which post to pull.

I have tried everything that should work but does not. My Controllers whose URL am rewritting using those rules are in module called blog. Is there supposed to be a special way of treating URL in module? AFAIK it should not be a problem but then, I might be wrong!

Well I found out that I was wrong and here is what works, thanks to this SO post!




 '<module:blog>/<controller:\w+>/<id:\d+>-<slug:[A-Za-z0-9 -_.]+>' => '<module>/<controller>/view',