如何在 OctoberCMS 插件中创建我的事件并在控制器中触发它?
How can I create my event in OctoberCMS plugin and fire it in the controller?
我试图在论坛中对问题进行投票后触发事件,以通知问题作者有关新开发的信息。 CMS 文档对于监听事件非常清楚,但对于自己创建事件却不清楚。所以我以 laravel 的方式创建了事件,如下所示:
author/myplugin/classess/events/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');
}
}
然后在boot()方法里面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...
}
});
}
在我的控制器中
Event::fire(new QuestionVotedEvent($question,$vote,$voter,$url));
侦听器未按预期将数据传递到数据库 table。问题是处理程序没有处理事件。
我还在侦听器 $event->question 中尝试过将数据从事件容器传递到处理程序,但没有成功。
我的问题似乎出在听众身上。尝试收听我的活动时,我似乎听不懂。如何解决这个问题?这是正确的方法还是您会建议更好的方法,我将不胜感激。
我不确定你没有找到这部分,它很容易触发和监听事件。
实际上你不需要创建事件你只需要 listen for them
和 fire at some point
[如果你没有任何复杂的工作流程就可以]
让我把你的代码转换成..那样
Create an event in-short listening for event
you need to write this code inside your plugin's Plugin.php
file's boot
method
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.
}
}
}
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
如需更多参考,您可以使用此 link:https://octobercms.com/docs/services/events
如果遇到任何问题,请发表评论。
我试图在论坛中对问题进行投票后触发事件,以通知问题作者有关新开发的信息。 CMS 文档对于监听事件非常清楚,但对于自己创建事件却不清楚。所以我以 laravel 的方式创建了事件,如下所示:
author/myplugin/classess/events/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');
}
}
然后在boot()方法里面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...
}
});
}
在我的控制器中 Event::fire(new QuestionVotedEvent($question,$vote,$voter,$url));
侦听器未按预期将数据传递到数据库 table。问题是处理程序没有处理事件。 我还在侦听器 $event->question 中尝试过将数据从事件容器传递到处理程序,但没有成功。
我的问题似乎出在听众身上。尝试收听我的活动时,我似乎听不懂。如何解决这个问题?这是正确的方法还是您会建议更好的方法,我将不胜感激。
我不确定你没有找到这部分,它很容易触发和监听事件。
实际上你不需要创建事件你只需要 listen for them
和 fire at some point
[如果你没有任何复杂的工作流程就可以]
让我把你的代码转换成..那样
Create an event in-short listening for event
you need to write this code inside your plugin'sPlugin.php
file'sboot
method
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.
}
}
}
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
如需更多参考,您可以使用此 link:https://octobercms.com/docs/services/events
如果遇到任何问题,请发表评论。