G
G
GONJY MONJY2021-01-14 08:48:28
Yii
GONJY MONJY, 2021-01-14 08:48:28

How to properly set up a route in urlManager?

Hello! I have a module on my site where a list of articles is displayed by a separate controller. The articles themselves have this link:
static.com/page/article/post.html?slug=test-music-title , where page is the module, article is the controller, post is the view, test-music-title is the CNC(slug parameter ).

I want to shorten the link to this:
static.com/page/test-music-title.html

Also it should work with other NCs for other articles.

In the urlManager I wrote this:

'page/<slug:\w+>.html' => 'page/article/post.html?slug=<slug:\w+>'

And here it is:
'page/<slug:\w+>.html' => 'page/article/post.html?<slug:\w+>'

I changed "w" to other characters, but it did not help. I read the documentation, but I did not find a solution to my question there.

Please explain: why this line does not work, and how can I write it correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2021-01-14
@GONJY_MONJY

'page/<slug:\w+>.html' => 'page/article/post.html?slug=<slug:\w+>'

Let's just analyze, the key (the first text) is needed in order to accept the url template; there you write what url you want to see.
In the value (second text) you write the path where to send this request without regular expressions.
Therefore, you should end up like this:
'page/<slug:[\w\-_\d]+>.html' => 'page/article/post'

I won’t say for sure right now, but if this is a module there are some subtleties, try to put it in the value before page /
like this:
'page/<slug:[\w\-_\d]+>.html' => '/page/article/post'

Accordingly, in actionPost, you need to specify the slug variable as an argument

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question