yii2 url 规则,命名参数为数组类型 [英] yii2 url rule with named parameter of type array

查看:49
本文介绍了yii2 url 规则,命名参数为数组类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个带有数组类型命名参数的 url 规则?

how can i create an url rule, with a named parameter of type array?

现在我有了这个,正在工作:

now i have this, working:

    'urlManager' => [
        'rules' => [
            'page/<id:\w+>' => 'cms/page/view',
        ],
    ],

将'page/welcome'映射到module=cmscontroller=pageaction=view、param $id=欢迎

to map 'page/welcome' to module=cms, controller=page, action=view, param $id=welcome

现在我想要这样的 url,具有可变数量的文件夹名称部分:/page/folder1/folder2/.../folderN/pagefile

now i would like to have the url like this, with a variable number of folder-name parts: /page/folder1/folder2/.../folderN/pagefile

对于一个动作:

function actionPage(array $folders, string $id) {
    // expecting:
    //   $folders == [ 'folder1', 'folder2', ..., 'folderN']
    //   $id      == 'pagefile'
}

不知道怎么写规则.我找不到有关规则语法的任何文档.

don't know how to write the rule. i couldn't find any documentation for the rule-syntax.

也欢迎使用变通方法!

推荐答案

据我所知,传递给控制器​​动作的参数必须是字符串或整数,因为控制器动作参数适用于简单的 GET 参数.

As far as I know the arguments passed to a controller action must be string or int because the controller action parameters work on simple GET parameters.

如果你真的想在 GET 请求中传递一个文件夹数组,它应该在路由之后,比如:controller/action/id?folder[]=folder1name&folder[]=folder2name

If you really want to pass an array of folders in a GET request it should be after the route, like: controller/action/id?folder[]=folder1name&folder[]=folder2name

因此 $id 将在控制器操作的范围内,但您必须手动"通过 $_GET['folder'] 获取查询中可变数量的文件夹字符串.

So $id would be in scope in the controller action but you would have to "manually" go through $_GET['folder'] to fetch the variable number of folders that are in the query string.

这篇关于yii2 url 规则,命名参数为数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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