Laravel 事件:如何在调用控制器方法后触发并将值传递给侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16746920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:48:59  来源:igfitidea点击:

Laravel Event: how to fire and pass value to a listener after a controller method is called

phplaravellaravel-4eloquent

提问by HymanpotK

In this case, we have 3 tables:

在这种情况下,我们有 3 个表:

tasks (task_id, task_title,task_type)
managers (manager_id, manager_title, manager_type)
assigners (task_id, manager_id)

tasks (task_id, task_title,task_type)
managers (manager_id, manager_title, manager_type)
assigners (task_id, manager_id)

In TaskController.php, I have a store method to create new task

TaskController.php,我有一个 store 方法来创建新任务

public function store() {  
    $input = Input::all();
    $this->task->create($input);
     return Redirect::route('tasks.index');}

Here's what I want to achieve:

这是我想要实现的目标:

  1. Pass the newly created task value to an event task.created,
  2. Compare the new task title $task->task_titlewith any $manager->manager_title, if any matches found ($task->task_titleLIKE $manager->manager_title), then pass the matched $task->task_idand $manager->manager_idas an array to another event,
  3. In an event listener located in start/global, it will create new record(s) in assigners table with values received from step2.The event listener is written as below:

    Event::listen('task.created',function($param1,$param2){  
        $new_assigner = new Assigner;  
        $assigner->task_id = $param1;  
        $assigner->manager_id = $param2;  
        $new_assigner->save();  
    });
    
  1. 将新创建的任务值传递给事件task.created
  2. 将新任务标题$task->task_title与 any进行比较$manager->manager_title,如果找到任何匹配项($task->task_titleLIKE $manager->manager_title),然后将匹配项$task->task_id$manager->manager_id作为数组传递给另一个事件,
  3. 在位于 start/global 的事件侦听器中,它将使用从 step2 接收到的值在分配表中创建新记录。 事件侦听器编写如下:

    Event::listen('task.created',function($param1,$param2){  
        $new_assigner = new Assigner;  
        $assigner->task_id = $param1;  
        $assigner->manager_id = $param2;  
        $new_assigner->save();  
    });
    

My questions:

我的问题:

  1. Where to fire the event in step 1 (in TaskController?) and what parameter should be used as a value to pass ($this->task?)
  2. How to achieve step 2? (there maybe more than one matches found)
  1. 在哪里触发第 1 步中的事件(在 TaskController 中?)以及应该使用什么参数作为要传递的值($this->task?)
  2. 如何实现第2步?(可能找到了不止一个匹配项)

回答by tharumax

  1. Since you need newly created task, it should be after the task has been created. May be after this line. You can pass newly created task as it is or, pass the task_id and fetch it in the event listener. But you have grab the task first.

    $task = $this->task->create($input);
    Event::fire('task.created', array($task) );
    
  2. Using for each loop.

    Event::listen('task.created', function($task)
    {
        $managers = getManagersForThisTask($task);
    
        foreach($managers as $manager ){
            Event::fire('task.created_step2', $task, $manager );
        }
    });
    
  1. 由于您需要新创建的任务,因此应该在任务创建之后。可能在这条线之后。您可以按原样传递新创建的任务,或者传递 task_id 并在事件侦听器中获取它。但你必须先抓住任务。

    $task = $this->task->create($input);
    Event::fire('task.created', array($task) );
    
  2. 用于每个循环。

    Event::listen('task.created', function($task)
    {
        $managers = getManagersForThisTask($task);
    
        foreach($managers as $manager ){
            Event::fire('task.created_step2', $task, $manager );
        }
    });