drz5553
2015-03-05 15:37
浏览 117
已采纳

Laravel。 将参数传递给事件订阅者

Im developing a task system and i want to mark as viewed a task and do other things when a user request the task

I wrote a Task Event Subscriber like this

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

And my controller I fire the event like this:

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);
}

But i dont know how can I or I should catch a parameter to be used inside of the event

By the way my Task Event subscriber is register on this Service Provider

class AthenaServiceProvider extends ServiceProvider {

    public function register()
    {
        // A lot of stuffs
    }

    public function boot()
    {
        \Event::subscribe('Athena\Events\UserEventSubscriber');
        \Event::subscribe('Athena\Events\TaskEventSubscriber');
    }

}

If you need more information just let me know, Thnks in advance

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个任务系统,我希望将用户标记为任务并在用户请求任务时执行其他操作

我写了一个这样的任务事件订阅者

 &lt;?php命名空间Athena \ Events; 
 
class TaskEventSubscriber {
  
 
公共函数onCreate($ event)
 {
 //这里我们可以发送大量电子邮件
 
 
 
公共函数onUpdate($ event)
 {
 \ Log:  :info('这是一些有用的信息。'); 
} 
 
公共函数onShow($ event)
 {
 \ Log :: info('视图事件现在被触发'); 
  } 
 
 
公共函数订阅($ events)
 {
 $ events-&gt; listen('user.create','Athena \ Events \ TaskEventSubscriber @ onCreate'); 
 
 $ events-  &gt; listen('user.update','Athena \ Events \ TaskEventSubscriber @ onUpdate'); 
 
 $ events-&gt; listen('task.show','Athena \ Events \ TaskEventSubscriber @ onShow'); \  n} 
} 
   
 
 

我的控制器我开火了 像这样的事件:

  public function show($ id)
 {
 $ canView = $ this-&gt; canView($ id); 
 if($ canView  !== true)
 {
返回$ canView; 
} 
 
 $ task = $ this-&gt; task-&gt; byId($ id); 
 //我们触发显示的事件
  $ this-&gt; events-&gt; fire('task.show',$ this-&gt; task); 
返回View :: make('tasks.show')
  - &gt; with('task',  $ task); 
} 
   
 
 

但我不知道我怎么能抓住在事件中使用的参数

顺便说一句,我的Task Event订阅者在此服务提供者上注册

 类AthenaServiceProvider扩展ServiceProvider {
 
公共函数寄存器()
 
 \  n //很多东西
} 
 
公共函数boot()
 {
 \ Event :: subscribe('Athena \ Events \ UserEventSubscriber'); 
 \ Event :: subscribe('Athena \  Events \ TaskEventSubscriber'); 
} 
   
 
 

}

如果您需要更多信息,请告诉我们,Thnks in 预先

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongxiang5879 2015-03-05 17:34
    已采纳

    I dont know how to feel already... I am passing the values what i need to my event there are on $event variable of each function

    Note: if you need to send more than one value just put them into an array and set your event function to catch them like parameters

    点赞 打赏 评论
  • dounielong7728 2015-06-28 09:12

    You will need to declare an Event object like this

    <?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;
        }
    }
    

    When you trigger the event with

    \Event::fire(TriggerShowTask, $this->task);
    

    The $taskobject will be passed to the event

    You can then access it in the subscriber with

    public function onShow($event)
    {
        $event->task; // A Task object
        \Log::info('The view event is now triggerd ');
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题