How to configure urlManager to get article name in the url

Hi guys,

In Yii2 advanced I have the following case: I have article and category article.

Is there a way to configure urlManager, add a class so that the URL to be something like:

http://www.domain.com/[article_category]/[article_name] ?

Thanks in advance for support,

Armand

a simple rule should do it, add a rule in your urlManager like so


'<category:[\w-]+>/<id:\d+>/<article:[\w-]+>'=>'article/index',


// then in your controller action you can access it as

public function actionIndex($category, $article)

{


}



make sure your urls are formatted properly

The issue with the above approach is, unless I’m mistaken it would conflict with the standard controller/action route. I’m guessing you’d want to create a rule class (http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html), prioritise it above the controller/action route, query the database to determine if it’s a correct article route base on the category/article param, and if not, continue.

You’d probably want to cache the database query as well, since it is a bit of unnecessary overhead querying the database every time before even knowing where to route the request.

it might conflict with existing routes, I just answered his question