简体   繁体   English

在前端/控制器中调用 yii2 控制台/控制器

[英]Calling yii2 console/controller inside frontend/controllers

I want to be able to trigger a console controller within my frontend/controllers namespace and also be able to pass alongside some parameters so that the request runs at the background with meddling with whatever the user is doing on the frontend.我希望能够在我的前端/控制器命名空间中触发控制台控制器,并且还能够传递一些参数,以便请求在后台运行,并干预用户在前端所做的任何事情。

Here is my console command这是我的控制台命令

<?php
namespace console\controllers;

use common\models\ApplicationStat;
use Yii;
use yii\console\Controller;
use yii\console\ExitCode;

class CronController extends Controller
{
    public $ain_number;
    public $din_number;
    public $userId;

    public function actionVerifyIdentity()
    {
        $model = new IdentityVerification();
        $model->applicant_id = $this->userId;
        $model->ain_status = $this->ain_number;
        $model->din_status = $this->din_number;
        $model->save();

        return ExitCode::OK;

    }

}

Within my frontend/controllers action, I am calling the snippet below to trigger the actionVerifyIdentity() in the console controller.在我的前端/控制器操作中,我调用下面的代码段来触发控制台控制器中的 actionVerifyIdentity()。

Yii::$app->runAction('cron/verify-identity', [
                'din_number' => 1611515151,
                'ain_number' => 118818181,
                'userId' => 7893
            ]);

How can I achieve this as what I wrote above is showing page not found and apparently it looks as if it can only working within the console folder?我怎样才能实现这一点,因为我上面写的内容显示未找到页面,显然它看起来好像只能在控制台文件夹中工作?

You should follow @rob006 instructions.您应该遵循@rob006 说明。 But still if you want to use the controller action directly then you can do this way -但是如果你想直接使用控制器动作,那么你可以这样做 -

$identityVerfier = new CronController();
$identityVerfier->applicant_id = 7893;
$identityVerfier->ain_status = 118818181;
$identityVerfier->din_status = 1611515151;
$identityVerfier->actionVerifyIdentity();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM