如何在OctoberCMS插件中创建我的事件并在控制器中触发它?

时间:2018-04-17 18:01:50

标签: laravel events octobercms

我试图在论坛上投票问题后发布一个事件,以通知问题作者有关新开发的问题。 CMS文档非常清楚地了解事件,而不是自己创建事件。所以我用laravel的方式创建了这个事件如下:

作者/为myplugin /类新/活动/ QuestionVotedEvent.php

<?php namespace Author\Myplugin\Classes\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class QuestionVotedEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */

    public $question;
    public $voter;
    public $vote;
    public $url;


    public function __construct($question,$voter,$vote,$url)
    {
        $this->question = $question;
        $this->voter = $voter;
        $this->vote = $vote;
        $this->url = $url;

    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

然后在 myplugin

内的boot()方法中
Event::listen('author.myplugin.QuestionVoteEvent', function($question,$vote,$voter) {

    $author = Question::where('id',$question)->first();
    if(!$author)
    {
        return false;
    }
    //Check if the is an upvote and award rps to the author of the question
    if($vote==1)
    {
        //Get rps type
        $rpsType = RPSType::where('code','UV')->first();
        $rps = new RPS;
        $rps->from_user_id = $voter;
        $rps->to_user_id = $author->user_id;
        $rps->rps_type_id = $rpsType->id;
        $rps->rps = $rpsType->points;
        $rps->url = $url;
        $rps->save();
        //Next send notfication to the user 
        //mailing logicgoes here...

    }




});

    }

在我的控制器中 Event :: fire(new QuestionVotedEvent($ question,$ vote,$ voter,$ url));

listner没有按预期将数据传递到数据库表中。事情是处理程序没有处理事件。 我还在侦听器 $ event-&gt;问题中尝试了这一点,以便将事件容器中的数据传递给处理程序,但没有成功。

我的问题似乎是听众。在试图听我的活动时,我似乎没有做对。怎么解决这个?这是正确的方式,或者你会建议一个更好的方式,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我不确定你是否找不到这部分,它很容易开火和听事件。

实际上,您不需要创建仅listen for themfire at some point的活动[如果您没有任何复杂的工作流程,它将会有效]

让我将您的代码转换为..就像那样

  

创建一个简短的事件监听事件   您需要在插件 Plugin.php 文件 boot方法

中编写此代码
use Event;  // add this to top if needed 

class Plugin extends PluginBase
{
    [...]

    public function boot()
    {
        Event::listen('author.myplugin.QuestionVoteEvent', function ( 
           $question,
           $vote,
           $voter,
           $url
        ) {
            // your code 
            // [$question, $vote, $voter, $url] all 4 variable will be available here.
        }
    }
}
  

现在控制器内部直接启动它

use Event; // add this to top if needed 


// from action fire event
Event::fire('author.myplugin.QuestionVoteEvent', [$question, $vote, $voter, $url]);

// note: this all 4 variables [$question, $vote, $voter, $url] will be passed
// to that event listener function as arguments and you can receive data there

如需更多参考,您可以使用以下链接:https://octobercms.com/docs/services/events

如果有任何问题请发表评论。