Yii2:如何将console命令中的命名参数传递给另一个动作?
问题描述:
I'm running the following console command:
yii t/gen 520 34 -someoption --number=1
and since t/gen
this is just an alias to the actual action template/generate-preview
I need to pass it on, or redirect, to another controller/action. So I do this:
Yii::$app->runAction('template/generate-preview', [ $ID, $count ]);
So the numbers 520 and 34 are passed on but how do I pass on the named parameters someoption
and number
? They are options in the actual controller and therefore public properties of that controller (like here).
Is it possible pass on those named parameters, that is, set those properties on the controller class?
答
You can use key-value pairs in parameters list:
Yii::$app->runAction('template/generate-preview', [
$ID,
$count,
'someoption' => true,
'number' => 1
]);
And do not add --
prefixes to parameters names, they will be prepended automatically.