Wai*_*ein 2 php laravel laravel-5 laravel-events laravel-5.7
我正在使用 Laravel 框架开发一个 Web 应用程序。我正在尝试在我的应用程序中使用事件和侦听器。但事件已被触发,并且触发事件的侦听器未被触发。
这是我的控制器操作
public function store(Request $request)
{
//other code
$item = Item::create($request->all())
broadcast(new ItemCreated($item));
return "Item created successfully";
}
Run Code Online (Sandbox Code Playgroud)
这是我的 Events\ItemCreated.php
class ItemCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $item;
public function __construct($item)
{
$this->item = $item;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我就有了该事件的监听器。
听众/EmailSubscribedUsers.php
class EmailSubscribedUsers
{
public function __construct()
{
//this constructor is not triggered
}
public function handle(ItemCreated $event)
{
//This method is not fired
}
}
Run Code Online (Sandbox Code Playgroud)
在 EventServiceProvider 中,我像这样注册了事件和监听器
protected $listen = [
ItemCreated::class => [
EmailSubscribedUsers::class
]
];
Run Code Online (Sandbox Code Playgroud)
事件被触发。但听众并没有被解雇。为什么?怎么了?
我尝试了以下解决方案。
php artisan optimize
composer dumpautoload
php artisan clear-compiled
Run Code Online (Sandbox Code Playgroud)
对不起大家。问题是我正在进行单元测试。在单元测试中,如果我使用 Event::fake(),则不会触发事件侦听器。我想测试事件监听器中的逻辑。因此,我删除了 Event::fake() 并测试了侦听器中的逻辑。
| 归档时间: |
|
| 查看次数: |
7978 次 |
| 最近记录: |