问题
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