我正在使用Yii2,希望使用urlManager进行路由,将所有非字母和非数字的字符转换为斜杠。我查看了许多已经提出的问题(#1, #2, #3, #4),但没有一个解决了我的问题,因为它们要么有点相似但不是我想要的,要么根本无法工作。
我有简单的urlManager规则:
在我的情况下,我的丑陋的URL是这样的(
我有简单的urlManager规则:
//...
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
.htaccess(也很简单):
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
在我的情况下,我的丑陋的URL是这样的(
SiteController -> public function actionTestRouter()
):
使用我之前编写的规则,我得到了更好的结果(因为它删除了localhost/frontend/web/index.php?r=site%2Ftest-router&ident=10&token=ADB&module=P120
index.php?r=
并将%2F
转换为/
):
localhost/frontend/web/site/test-router/ident/10/token/ADB/module/P120
我想要得到的是:
localhost/frontend/web/site/test-router/ident/10/token/ADB/module/P120
我尝试的几个规则是:
'test-route/<ident:\d+>/<token:\w+>/<module:\w+>' => 'test-route' // 1
'<controller:\w+>/<action:\w+>/<ident:\d+>/<token:\w+>/<module:\w+>' => '<controller>/<action>' // 2
'<controller:\w+>/<action:\w+>/<slug:[a-zA-Z0-9_-]+>/' => '<controller>/<action>/<slug>' // 3 (not even sure what slug does here
如果规则适用于任何参数和值,无论其名称和值如何,那将非常好。
CUrlManager -> parsePathInfo ($pathInfo)
方法中实现。因此,在 Yii 2 中,我们必须通过自己的实现或从 Yii 1 中进行重用来解决问题。 - FantomX1