How Can I Create My Event In OctoberCMS Plugin And Fire It In The Controller?

- 1 answer

I trying to fire an event after a question is voted on in the forum to notify the question author about the new development. The CMS documentation is very clear about listening to events but not about creating the event themselves. So I created the event the laravel way as follows:


<?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');

Then in boot() method inside myplugin

Event::listen('author.myplugin.QuestionVoteEvent', function($question,$vote,$voter) {

    $author = Question::where('id',$question)->first();
        return false;
    //Check if the is an upvote and award rps to the author of the question
        //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;
        //Next send notfication to the user 
        //mailing logicgoes here...




In my controller Event::fire(new QuestionVotedEvent($question,$vote,$voter,$url));

The listner is not passing data into the database table as expected. The thing is the handler is not handling the event. I have also tried this in the listener $event->question to pass the data from the event container to the handler without success.

My problem seem to be at the listener. I seem not be getting it right when trying to listen to my event. How would resolve this? Is this the right way or you would suggest a better way, I will appreciate thanks.



I am not sure you didn't find this portion, its pretty easy to fire and listen events.

actually you don't need to create events you just listen for them and fire at some point [it will work if you don't have any complex work flow]

let me convert your code to.. like that

Create an event in-short listening for event
you need to write this code inside your plugin'sPlugin.phpfile'sboot method

use Event;  // add this to top if needed 

class Plugin extends PluginBase

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

Now inside controller fire it directly

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

for more reference you can use this link :

if getting any issue please comment.