After some consideration the way I chose to call one controller from within another was by using the runAction method of the controller (which is also the recommended way by the Yii developers).
Example for a console application:
\Yii::$app->runAction('webserver/update-config');
It is also possible to hand over params by using an array in as second parameter.
An example for simple parameters:
\Yii::$app->runAction('webserver/update-config', ['oneValue', 'anotherValue'];
Here an example for named parameters:
\Yii::$app->runAction('webserver/update-config', [
'servertype' => 'oneSetting',
'serverdir' => 'anotherSettingValue'
]);
Please note that this makes the called controller a part of the calling code. So if the called controller fails for some reason the whole program fails.
Good error handling is a must. In the called controller you can set the error code to give back by using return.
Example:
Calling line of code:
$iExitCode = \Yii::$app->runAction('webserver/update-config', ['oneValue', 'anotherValue'];
Called controller:
<?php
namespace app\commands;
use yii\console\Controller;
/**
* Webserver related functions
*/
class WebserverController extends Controller {
public function actionUpdateConfig($oneValue, $anotherValue) {
// Code that does something
if ($success) return 0;
else return 1;
}
}
?>