Yii2:在控制台控制器中运行 Web 操作 [英] Yii2: Run a web action in a console controller

查看:36
本文介绍了Yii2:在控制台控制器中运行 Web 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在控制台控制器中运行网络操作?

Is it possible to run a web action in a console controller?

在 web 控制器 (yii\web\Controller) 中,我有一个想要作为 cron 作业运行的操作.但是如果我尝试在控制台控制器 (yii\console\Controller) 中运行它:

In a web controller (yii\web\Controller) I have an action which I would like to run as a cron job. But if I try to run it in a console controller (yii\console\Controller):

Yii::$app->runAction('controller/action');

然后我收到以下错误消息:

then I receive the following error message:

Error: Unknown command "controller/action".

推荐答案

抱歉,我没有理解问题的上下文.

Sorry, I did not understand the context of the question.

正如@soju 所说,您应该找到一种使用 CURL 的方法,但有一种方法.

As @soju says, you should find a way to do it using CURL, but there is a way.

$config = \yii\helpers\ArrayHelper::merge(
            require(Yii::getAlias('@common').'/config/main.php'),
            require(Yii::getAlias('@common').'/config/main-local.php'),
            require(Yii::getAlias('@frontend').'/config/main.php'),
            require(Yii::getAlias('@frontend').'/config/main-local.php')
        );

$web_application = new \yii\web\Application($config);
$web_application->runAction('/site/index',['param1' => 1,'param2' => 2]);

你应该知道控制器处理他们的行为,然后 AccessControl 可以阻止执行

You should be aware that the controller works with their behaviors, then the AccessControl could prevent the execution

这篇关于Yii2:在控制台控制器中运行 Web 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆