Yii2 url manager don't parse urls with get par

2019-07-20 18:40发布

I've created a module named catalogue with, for the moment, two actions in the default controller:

  1. actionIndex
  2. actionLineProducts

in the index view I have some link which run the line-product's action, the url was the result of:

Url::to(['line-products', 'line' => $line->name])

my goal is to obtain a link like

catalogue/{line-name}

where line-name is the parameter I send to action LineProducts. My urlManager configurations are:

'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
      '<moduls:catalogue>/<controller:default>/<action:line-products>/<line:*>' => '<module>/<line>',
      '<controller:\w+>/<id:\d+>' => '<controller>/view/',
      '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
      '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
 ],

.htaccess configurations:

RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

Can anyone explain me why my URL is always like:

http://my-site.dev/catalogue/default/line-products?line={line-name}

1条回答
疯言疯语
2楼-- · 2019-07-20 19:21

You swapped the configuration. The key is the pattern of the url as passed from the browser. The value is the route. Since you are using a specific module, controller and action you can pass those in directly:

'rules' => [
    'catalogue/<line>' => 'catalogue/default/line-products'
    ...
]

You can read the Routing and Url Creation page in the Yii2 guide for more information.

查看更多
登录 后发表回答