YII2 - 为cli命令注册事件

时间:2017-11-29 15:01:13

标签: php yii

我制作了 BidController 组件,我将用于crone作业。

<?php

namespace app\commands;

use Yii;

use app\models\Bid;
use app\components\Bids;

use yii\console\Controller;

    class BidController extends Controller
    {
        public function actionExpired()
        {
            $bids = Bid::find()
                ->where(['<', 'expiration_date', 'CURDATE()'])
                ->all();

            if($bids)
                Bids::expired($bids);
        }

    }

我尝试在 web.php 配置文件中注册该事件,在组件

'bid' => [
            'on bid-response'   => ['app\components\Notifier', 'handleBidResponse'],
        ],

这是我尝试触发事件的方式:

Yii::$app->bid->trigger(UserEvents::BID_RESPONSE, new UserEvents([

            'user'    => $user,
            'title'   => $title,
            'message' => $message,

        ]));

尝试运行命令时出现此错误:

Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: yii\console\Application::bid'

UserEvents 类:

<?php

namespace app\components;

use yii;
use yii\base\Event;

class UserEvents extends Event
{
    const REGISTER      = 'register';
    const PAYMENT       = 'payment';
    const EXPIRE        = 'expire';
    const NOTIFICATION  = 'notification';
    const TOGGLEPUBLISH = 'toggle-publish';
    const WISHLIST      = 'wishlist';
    const BID           = 'bid';
    const BID_RESPONSE  = 'bid-response';
    const NEW_PRODUCT   = 'new-product';

    public $user;
    public $type    = null;
    public $message = null;
    public $title   = null;
    public $admin   = false;
}

如何为cli命令正确注册事件?

0 个答案:

没有答案