S
S
skyfortress2014-10-03 01:03:16
Yii
skyfortress, 2014-10-03 01:03:16

How to generate friendlyUrl for link in Yii2?

I am learning yii2 and faced the problem of creating CNC. As I understand it, in urlManager, you can specify the rules by which the link is parsed into parameters and passed in acc. action, but I have figured out how to get the reverse process. The /helper/url class makes links like "/blog?category=other", not "/blog/other".
My config:

'urlManager' => [
  'enablePrettyUrl' => true,
  'showScriptName' => false,
  'rules' => [
    '<action:>' => 'site/<action>',
                '<action:[\w\-]+>/<category:[\w\-]+>' => 'site/<action>',
  ],
],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
skyfortress, 2014-10-05
@skyfortress

Solved the problem myself.

'rules' => [
        '<action:(contact|about)>' => 'site/<action>',
        'blog/<category>/<post>'=>'site/post',
        'blog/<category>'=>'site/blog',
        'blog'=>'site/blog',
],

S
Sergey, 2014-10-03
Protko @Fesor

with the problem of creating CNC

with the problem of routing requests ... CNC is machine tools.
Make sure to read the documentation before asking questions. Surely there will be some methods like createUrl('site/action', ['category'=>'name'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question