/ / Come posso creare il mio evento nel plugin OctoberCMS e lanciarlo nel controller? - laravel, eventi, ottobre

Come posso creare il mio evento nel plugin OctoberCMS e accenderlo nel controller? - laravel, eventi, octobercms

Sto cercando di lanciare un evento dopo una domandavotato nel forum per informare l'autore della domanda sul nuovo sviluppo. La documentazione CMS è molto chiara sull'ascolto di eventi ma non sulla creazione dell'evento stesso. Quindi ho creato l'evento nel modo laravel come segue:

Autore / myplugin / classess / eventi / QuestionVotedEvent.php

<?php namespace AuthorMypluginClassesEvents;

use IlluminateBroadcastingChannel;
use IlluminateQueueSerializesModels;
use IlluminateBroadcastingPrivateChannel;
use IlluminateBroadcastingPresenceChannel;
use IlluminateFoundationEventsDispatchable;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateContractsBroadcastingShouldBroadcast;

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 IlluminateBroadcastingChannel|array
*/
public function broadcastOn()
{
return new PrivateChannel("channel-name");
}
}

Quindi nel metodo boot () all'interno myplugin

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...

}




});

}

Nel mio controller Evento :: fuoco (nuovo QuestionVotedEvent ($ question, $ vote, $ voter, $ url));

Il listner non sta trasferendo i dati nella tabella del database come previsto. Il fatto è che il gestore non sta gestendo l'evento. Ho anche provato questo nell'ascoltatore $ Event-> domanda per passare i dati dal contenitore degli eventi al gestore senza esito positivo.

Il mio problema sembra essere all'ascoltatore. Mi sembra di non aver capito bene quando provo ad ascoltare il mio evento. Come risolverebbe questo? È questo il modo giusto o suggeriresti un modo migliore, apprezzerò grazie.

risposte:

1 per risposta № 1

Non sono sicuro che non hai trovato questa parte, è abbastanza facile sparare e ascoltare eventi.

in realtà non è necessario creare eventi proprio listen for them e fire at some point [funzionerà se non hai un flusso di lavoro complesso]

fammi convertire il tuo codice in .. così

Crea un evento in breve ascolto per l'evento
devi scrivere questo codice all'interno del plug-in Plugin.php File boot metodo

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.
}
}
}

Ora all'interno del controller sparalo direttamente

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

per ulteriori riferimenti è possibile utilizzare questo link: https://octobercms.com/docs/services/events

in caso di problemi, si prega di commentare.