我正在开发一个任务系统,当用户请求任务时,我想标记为已查看的任务并做其他事情
我写了一个类似的任务事件订阅服务器
<?php namespace Athena'Events;
class TaskEventSubscriber {
public function onCreate($event)
{
// Here we can send a lot of emails
}
public function onUpdate($event)
{
'Log::info('This is some useful information.');
}
public function onShow($event)
{
'Log::info('The view event is now triggerd ');
}
public function subscribe($events)
{
$events->listen('user.create', 'Athena'Events'TaskEventSubscriber@onCreate');
$events->listen('user.update', 'Athena'Events'TaskEventSubscriber@onUpdate');
$events->listen('task.show', 'Athena'Events'TaskEventSubscriber@onShow');
}
}
我的控制器触发了这样的事件:
public function show($id)
{
$canView = $this->canView($id);
if($canView !== true)
{
return $canView;
}
$task = $this->task->byId($id);
// We fire the showed event
$this->events->fire('task.show', $this->task);
return View::make('tasks.show')
->with('task', $task);
}
但我不知道我或我应该如何捕捉要在事件中使用的参数
顺便说一句,我的任务事件订阅者在这个服务提供商上注册
class AthenaServiceProvider extends ServiceProvider {
public function register()
{
// A lot of stuffs
}
public function boot()
{
'Event::subscribe('Athena'Events'UserEventSubscriber');
'Event::subscribe('Athena'Events'TaskEventSubscriber');
}
}
如果您需要更多信息,请告诉我,提前通知
您需要声明一个类似的Event对象
<?php namespace App'Events;
use App'Events'Event;
use Illuminate'Queue'SerializesModels;
class TriggerShowTask extends Event {
use SerializesModels;
public $task;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($task)
{
$this->task = $task;
}
}
当您使用触发事件时
'Event::fire(TriggerShowTask, $this->task);
$task
对象将传递给事件
然后,您可以使用在订户中访问它
public function onShow($event)
{
$event->task; // A Task object
'Log::info('The view event is now triggerd ');
}
我已经不知道该怎么想了。。。我正在将我需要的值传递给我的事件,每个函数的$event变量上都有
注意:如果您需要发送多个值,只需将它们放入一个数组中,并设置事件函数来捕获它们,就像参数