Yii2漂亮的URL:自动转换所有带斜杠的东西(包括所有参数) [英] Yii2 pretty URL: automatically convert everything with slashes (including all parameters)

查看:19
本文介绍了Yii2漂亮的URL:自动转换所有带斜杠的东西(包括所有参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Yii2,我想使用带有路由的 urlManager 将所有非字母和非数字字符转换为斜杠.我看了很多已经问过的问题(#1#2#3#4) 但没有解决它,因为它们要么显示有点相似但不是什么我愿意或根本不为我工作.

I'm working with Yii2 and I would like to use urlManager with routing to convert all non-letter and non-number characters into slashes. I have looked at a lot of question what have already been asked (#1, #2, #3, #4) but none solved it since they either show a little similar but not what I want or not working for me at all.

我有简单的 urlManager 规则:

I have simple urlManager rules:

//...
'urlManager' => [
    'class' => 'yiiwebUrlManager',
    '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(也很简单):

.htaccess (also simple):

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()):

In my case, my ugly URL is this (SiteController -> public function actionTestRouter()):

localhost/frontend/web/index.php?r=site%2Ftest-router&ident=10&token=ADB&module=P120

localhost/frontend/web/index.php?r=site%2Ftest-router&ident=10&token=ADB&module=P120

使用我上面写的规则,我得到了更好的结果(因为它删除了 index.php?r= 并将 %2F 转换为 />):

With rules I have written above, I get better result (because it removes index.php?r= and converts %2F to /):

localhost/frontend/web/site/test-router?ident=10&token=ADB&module=P120

localhost/frontend/web/site/test-router?ident=10&token=ADB&module=P120

我想得到什么:

localhost/frontend/web/site/test-router/ident/10/token/ADB/module/P120

localhost/frontend/web/site/test-router/ident/10/token/ADB/module/P120

我几次尝试使用规则是:

My several attemps with rules were:

'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

如果规则适用于任何参数和值,而不管它们的名称和值如何,那就太好了.

It would also be super nice if the rules would apply to any parameters and values, regardless of their name and values.

推荐答案

你的第二次尝试

'<controller:[w-]+>/<action:[w-]+>/<ident:d+>/<token:w+>/<module:w+>' => '<controller>/<action>' // 2

将采用/创建网址

localhost/frontend/web/site/test-router/10/ADB/P120

localhost/frontend/web/site/test-router/10/ADB/P120

url 中没有参数名称,这些参数将仅按此顺序使用,并且它们的列表如您所见是固定的

without names of params in url and these params will be used only in this order and their list is fixed as you see

如果您想在 url 中添加他们的名字(用于您的问题中的 estetic 或 seo 目的):

If you want add their names in url (for estetic or seo purposes like in your question):

'<controller:[w-]+>/<action:[w-]+>/ident/<ident:d+>/token/<token:w+>/module/<module:w+>' => '<controller>/<action>',  // 2

这些路由的 url 创建将相同:

And url creation for these routes will be same:

echo Url::to(['site/test-router', 'ident' => 100, 'module' => 100, 'token' => 100]);

如果你想解析这个参数列表的不同长度,你可以像这样使用 smth:

If you want parse various length of this list of params, you can use smth like this:

'<controller:[w-]+>/<action:[w-]+>/<params:[a-zA-Z0-9_-/]+>' => '<controller>/<action>'

或者只为一条路线指定:

or specify it only for one route:

'site/test-route/<params:[a-zA-Z0-9_-/]+>' => 'site/test-route'

所以在操作中你会得到参数 params: Yii::$app->request->get('params'); 用正则表达式解析它.

So in action you will get parameter params: Yii::$app->request->get('params'); parse it with regexp.

这篇关于Yii2漂亮的URL:自动转换所有带斜杠的东西(包括所有参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆