问题
I've created a module named catalogue with, for the moment, two actions in the default controller:
- actionIndex
- 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:
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.
来源:https://stackoverflow.com/questions/37836863/yii2-url-manager-dont-parse-urls-with-get-parameter