键入在Laravel听众课上暗示其他课程

发布于 2025-02-13 22:42:16 字数 1237 浏览 0 评论 0原文

我的活动听众定义为:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2025-02-20 22:42:16

您正在加载依赖项,必须遵循依赖注入规则。
在Laravel中,您可以在这样的控制器中使用依赖注入,因为这是由Laravel/Symfony构建的。

而是将您的依赖性添加到构造函数中。

use App\Events\LeadCreated;
use App\Services\LeadService;

class NewLeadNotifyProspectListener
{
    private LeadService $leadService; 

    public function __construct(LeadService $leadService)
    {
        $this->leadService = $leadService;
    }

    /**
     * Handle the event.
     *
     * @param \App\Events\LeadCreated $event
     * @return void
     */
     public function handle(LeadCreated $event)
    {
        $this->leadService->notifyProspect($event->lead);
    }
}

如果您正确设置了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.

use App\Events\LeadCreated;
use App\Services\LeadService;

class NewLeadNotifyProspectListener
{
    private LeadService $leadService; 

    public function __construct(LeadService $leadService)
    {
        $this->leadService = $leadService;
    }

    /**
     * Handle the event.
     *
     * @param \App\Events\LeadCreated $event
     * @return void
     */
     public function handle(LeadCreated $event)
    {
        $this->leadService->notifyProspect($event->lead);
    }
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文