Yii2 url manager don't parse urls with get parameter

只谈情不闲聊 提交于 2019-12-24 00:51:22

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!