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
.
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
.
On a site I'm overhauling, I have a need for a background task, that can be toggled via an action, which requires that I can also find its pid using ps. After much googling and almost as much swearing, I pieced together the solution.
Yes, I understand that shell_exec should be used with extreme caution, thanks.
You can either
exec()
your command ´´´php yii suggestions´´´ but this may result in permission issues with the webserver user.The better way is to use a ConsoleRunner extension, e.g. yii2-console-runner or yii2-console-runner-extension which do the job control job a little bit more sophisticated and more secure with
popen()
.Always be aware of code injections when executing
exec()
and the like!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
Set the alias to be available in the console\config\main.php [OPTIONAL]
Finally from the frontend view, make the call like this: In this case, calling the controller route fhir then method import()
This is way I found and used some time ago to run yii console controller/action (I used this for run migrations from web).
In your web controller action:
It can be done much simpler
Github LINK
I think this is the simplest solution: