键入在Laravel听众课上暗示其他课程
我的活动听众定义为:
use App\Events\LeadCreated;
use App\Services\LeadService;
class NewLeadNotifyProspectListener
{
/**
* Handle the event.
*
* @param \App\Events\LeadCreated $event
* @param App\Services\LeadService $leadService
* @return void
*/
public function handle(LeadCreated $event, LeadService $leadService)
{
$leadService->notifyProspect($event->lead);
}
}
和我的事件
use App\Models\Lead;
class LeadCreated
{
public Request $request;
public Lead $lead;
public function __construct(Lead $lead, Request $request)
{
$this->request = $request;
$this->lead = $lead;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
,并且我在控制器中称其为这样:
LeadCreated::dispatch($lead, $request);
我收到的错误:
太少争取功能应用程序\ listerer \ newleadnotifyProspectListListener :: handle(),1 in/vendor/laravel/framework/framework/src/illuminate/events/events/dispatcher.php on Line 424和2个预期
preenge
几乎没有争取 在我的侦听器中键入LeadService
?在我的听众中包括该课程的正确方法是什么?
I have my event listener defined as this:
use App\Events\LeadCreated;
use App\Services\LeadService;
class NewLeadNotifyProspectListener
{
/**
* Handle the event.
*
* @param \App\Events\LeadCreated $event
* @param App\Services\LeadService $leadService
* @return void
*/
public function handle(LeadCreated $event, LeadService $leadService)
{
$leadService->notifyProspect($event->lead);
}
}
And my event
use App\Models\Lead;
class LeadCreated
{
public Request $request;
public Lead $lead;
public function __construct(Lead $lead, Request $request)
{
$this->request = $request;
$this->lead = $lead;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
And I'm calling it in my controller like this:
LeadCreated::dispatch($lead, $request);
The error I'm receiving:
Too few arguments to function App\Listeners\NewLeadNotifyProspectListener::handle(), 1 passed in /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php on line 424 and exactly 2 expected
I wonder why I'm not able to type-hint LeadService
in my listener? What's the proper way to include that class in my listener?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在加载依赖项,必须遵循依赖注入规则。
在Laravel中,您可以在这样的控制器中使用依赖注入,因为这是由Laravel/Symfony构建的。
而是将您的依赖性添加到构造函数中。
如果您正确设置了
newleadnotifyProspectListener
,那么Laravel中的依赖项注入将在构造函数中注入该服务,就像它在控制器中所做的那样You are loading a dependency, you have to follow Dependency Injection rules.
In Laravel you can use Dependency Injection in Controllers like that, because that's build by Laravel/Symfony.
Add your dependency in the constructor instead.
If you have setup the
NewLeadNotifyProspectListener
correctly, Dependency Injection in Laravel will inject that service in the constructor, just like it would have done in a Controller