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
Laravel Event: how to fire and pass value to a listener after a controller method is called
提问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:
这是我想要实现的目标:
- Pass the newly created task value to an event
task.created
, - Compare the new task title
$task->task_title
with any$manager->manager_title
, if any matches found ($task->task_title
LIKE$manager->manager_title
), then pass the matched$task->task_id
and$manager->manager_id
as an array to another event, 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(); });
- 将新创建的任务值传递给事件
task.created
, - 将新任务标题
$task->task_title
与 any进行比较$manager->manager_title
,如果找到任何匹配项($task->task_title
LIKE$manager->manager_title
),然后将匹配项$task->task_id
和$manager->manager_id
作为数组传递给另一个事件, 在位于 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:
我的问题:
- Where to fire the event in step 1 (in TaskController?) and what parameter should be used as a value to pass ($this->task?)
- How to achieve step 2? (there maybe more than one matches found)
- 在哪里触发第 1 步中的事件(在 TaskController 中?)以及应该使用什么参数作为要传递的值($this->task?)
- 如何实现第2步?(可能找到了不止一个匹配项)
回答by tharumax
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) );
Using for each loop.
Event::listen('task.created', function($task) { $managers = getManagersForThisTask($task); foreach($managers as $manager ){ Event::fire('task.created_step2', $task, $manager ); } });
由于您需要新创建的任务,因此应该在任务创建之后。可能在这条线之后。您可以按原样传递新创建的任务,或者传递 task_id 并在事件侦听器中获取它。但你必须先抓住任务。
$task = $this->task->create($input); Event::fire('task.created', array($task) );
用于每个循环。
Event::listen('task.created', function($task) { $managers = getManagersForThisTask($task); foreach($managers as $manager ){ Event::fire('task.created_step2', $task, $manager ); } });