Run Console Command from View

I’m struggling to manage to run a console command from a view.

I setup a console controller with a common/component/…php and the command works perfectly in the windows command prompt, but I seem unable to get it to work through a view (I don’t have console access on this host, so I need another way to trigger it, hence wanting to use a button in a view). It reports back a #404 error?

I have been trying to use an action

$oldApp = \Yii::$app;
new \console\controllers\MaintenanceController(Yii::$app->controller->id, Yii::$app);
$result = \Yii::$app->runAction('backupDatabase');
\Yii::$app = $oldApp;

Could someone help me get this up and running?

I’ve also tried

    $oldApp = \Yii::$app;
    new \yii\console\Application([
        'id' => 'Command runner',
        'basePath' => '@app',
        'components' => [
            'db' => $oldApp->db,
        ],
    ]);
    \Yii::$app->runAction('maintenance/backupDatabase');
    \Yii::$app = $oldApp;

but this errs and returns

2019-11-20 11:04:37 [-][-][-][error][yii\console\UnknownCommandException] exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "maintenance/backupDatabase".' in Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\Module.php:537
Stack trace:
#0 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\console\Application.php(180): yii\base\Module->runAction('maintenance/bac...', Array)
#1 Z:\M\Websites\www.devsite.com\backend\controllers\TestingController.php(68): yii\console\Application->runAction('maintenance/bac...')
#2 [internal function]: backend\controllers\TestingController->actionIndex()
#3 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#4 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#5 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('index', Array)
#6 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('testing/index', Array)
#7 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#8 Z:\M\Websites\www.devsite.com\backend\web\index.php(17): yii\base\Application->run()
#9 {main}

Next exception 'yii\console\UnknownCommandException' with message 'Unknown command "maintenance/backupDatabase".' in Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\console\Application.php:183
Stack trace:
#0 Z:\M\Websites\www.devsite.com\backend\controllers\TestingController.php(68): yii\console\Application->runAction('maintenance/bac...')
#1 [internal function]: backend\controllers\TestingController->actionIndex()
#2 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#3 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#4 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('index', Array)
#5 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('testing/index', Array)
#6 Z:\M\Websites\www.devsite.com\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#7 Z:\M\Websites\www.devsite.com\backend\web\index.php(17): yii\base\Application->run()
#8 {main}

You should move code from console action backupDatabase to the separate class and/or function and use that in the console command and also in the view. It’s not good practice to run whole application again, to use some part of the code.

Faboslav,

Thank you for taking the time to answer.

That said, this is all very new to me and would need much more guidance if possible.

Currently, I created a Console Controller action that uses a Common Component function. Then I try to run the Console action in my view. I found the code online.

If this isn’t the proper approach, I have no objection starting over, but would need some serious guidance, or an example (if one exists).

If you have commonComponent with backupDatabase function, then in your console controller is something like this:

public function actionBackupDatabase() {
    $this->commonComponent->backupDatabase();
}

Then you have some probably controller with actions on the “frontend” which is rendering that view? So add same action to that “frontend controller” and call it from the view either as form submit or as ajax request.

Basically all you need to do is share that backup database functionality in some common component and then use that component in your console app controller action and frontend app controller action.