url yii2 中的嵌套控制器和操作
nested controllers and actions in url yii2
我有一个包含 tables teams 的数据库,其中包含字段 id 和 name,和 players,字段为 id,team_id 和 姓名。我想在 球员 table 中创造记录,然后再在 球队 table 中创造记录。
例如,我创建了一个团队,转到 edit - URL 看起来像这样: /teams/update?id=1
。在编辑部分,有 link 和文本 add player,它与控制器 Players Controller 相关联。现在 Players Controller 的 URL 看起来像 /players/create
(在创建部分),但我希望它是 /1/players/create
,其中 1 对应于team_id 值。我怎样才能做到这一点?
在 urlManager 设置中添加两条规则。
'rule' => [
...
"<id:\d+>/<module:\w+>/<controller:\w+>/<action:\w+>"=>"<module>/<controller>/<action>",
"<id:\d+>/<controller:\w+>/<action:\w+>"=>"<controller>/<action>"
]
等号左边是模式,等号右边是路线
“”中的id表示这样的动作的参数
public function actionCreate($id)
所以你可以把"id"改成"teams_id"
""表示匹配所有模块,其他两种模式相同
我有一个包含 tables teams 的数据库,其中包含字段 id 和 name,和 players,字段为 id,team_id 和 姓名。我想在 球员 table 中创造记录,然后再在 球队 table 中创造记录。
例如,我创建了一个团队,转到 edit - URL 看起来像这样: /teams/update?id=1
。在编辑部分,有 link 和文本 add player,它与控制器 Players Controller 相关联。现在 Players Controller 的 URL 看起来像 /players/create
(在创建部分),但我希望它是 /1/players/create
,其中 1 对应于team_id 值。我怎样才能做到这一点?
在 urlManager 设置中添加两条规则。
'rule' => [
...
"<id:\d+>/<module:\w+>/<controller:\w+>/<action:\w+>"=>"<module>/<controller>/<action>",
"<id:\d+>/<controller:\w+>/<action:\w+>"=>"<controller>/<action>"
]
等号左边是模式,等号右边是路线
“
public function actionCreate($id)
所以你可以把"id"改成"teams_id"
"