Hello to all! In his project on Yii2 implemented user-friendly (CNC) links, as described in the article here . I write alias: 'test-alias.html' in the database, and I write 'site / about' in the route and everything works on Ur. But if you write a route in the database type 'category / 2', the CNC link is not formed (just goes to mysite.com/ru/category/2). My link is formed like this:

Url::to(['category/view', 'id' => $hit->id]) 

and in UrlManager there is a rule:

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

How to make so that when you click on the link type "category / 2" was formed and opened CNC link?

  • Show all the rules. And how to understand the phrase "CNC link was formed and opened?" - Yaroslav Molchan
  • Here are all the rules prntscr.com/dcnido - HugeD

2 answers 2

You need to create a link via Url::toRoute()

 Url::toRoute(['category/view', 'id' => $hit->id]) 

Url::toRoute() takes a link from the route map while Url::to() takes a string as a link

    Rule 'category/<id:\d+>' => 'category/view' , says that url site.com/category/7 needs to call the action of the view controller category and pass it the id parameter of the integer type. the reverse is also true: if you specify the category / view route and specify the id parameter, then Url :: to () will generate a category / id type URL.

    and you are trying to create an url by specifying the wrong route. Route can not be category / 2. There must be an action in the route (with the controller, parameters and module if necessary)

    try to understand once again https://github.com/yiisoft/yii2/blob/master/docs/guide-ru/runtime-routing.md and understand everything)

    to the previous answer: Url :: to () also builds the URL along the route (if an array is passed) or simply displays the string