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

查看:390
本文介绍了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' => '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(也很简单):

.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

就我而言,我的丑陋网址是这个( SiteController- >公共功能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

如果您想在网址中添加其名称(出于诸如查询之类的用于美学或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

并为这些路线创建网址将会是相同的:

And url creation for these routes will be same:

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

如果您想解析此参数列表的各种长度,则可以使用以下命令: p>

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天全站免登陆