Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run console command in yii2 from web

I have created a console command in console/controllers with SuggestionController .

If i run command like php yii suggestions, its working.

I want to know how to execute console command from web without any extensions of yii2.

like image 270
saran varma Avatar asked Dec 14 '15 10:12

saran varma


1 Answers

As of Yii2 - 2.0.11.2 advanced app -- this works

First let's make sure controller and namespace correct. In this case frontend app accessing console application import method()

In console\controllers\FhirController

enter image description here

Set the alias to be available in the console\config\main.php [OPTIONAL]

enter image description here

'aliases' => [
    '@common' => dirname(__DIR__),
    '@frontend' =>  dirname(dirname(__DIR__)) . '/frontend',
    '@backend' =>  dirname(dirname(__DIR__)) . '/backend',
    '@console' =>  dirname(dirname(__DIR__)) . '/console',
],

Finally from the frontend view, make the call like this: In this case, calling the controller route fhir then method import()

$consoleController = new console\controllers\FhirController('fhir', Yii::$app); 
$consoleController->runAction('import');
like image 110
Gajen Sunthara Avatar answered Sep 21 '22 23:09

Gajen Sunthara