yii2 url rule with named parameter of type array

为君一笑 提交于 2019-12-11 10:17:12

问题


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',
        ],
    ],

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

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

for an action:

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.

workarounds are welcome, too!


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/43933317/yii2-url-rule-with-named-parameter-of-type-array

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