Laravel:对侦听器上的字符串调用成员函数send()
当一个新用户注册时,我试图执行Laravel:对侦听器上的字符串调用成员函数send(),laravel,laravel-5.5,Laravel,Laravel 5.5,当一个新用户注册时,我试图执行pushnotification。因此,当我触发一个事件事件(new MemberNotificationEvent($UserDetails)),我创建了名为MemberNotificationEvents的事件流正在完全运行,但在MemberNotificationListener上的公共函数句柄(MemberNotificationEvent$事件)返回以下错误: 对字符串调用成员函数send() 我将MemberNotificationListener的完整
pushnotification
。因此,当我触发一个事件事件(new MemberNotificationEvent($UserDetails)),我创建了名为MemberNotificationEvents
的事件在mysignUpController上的code>流正在完全运行,但在MemberNotificationListener上的公共函数句柄(MemberNotificationEvent$事件)
返回以下错误:
对字符串调用成员函数send()
我将MemberNotificationListener
的完整代码放入:
<?php
namespace App\Listeners;
use App\Events\MemberNotificationEvent;
use App\Services\PushNotificationService;
use Illuminate\Contracts\Queue\ShouldQueue;
class MemberNotificationListener implements ShouldQueue
{
private $pushNotificationService;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
$this->pushNotificationService = PushNotificationService::class;
}
private function getMessageBody($username)
{
return "Awesome! Welcome " . $username . " to IDM";
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(MemberNotificationEvent $event)
{
$username = $event->UserDetails->name;
$message = $this->getMessageBody($username);
$this->pushNotificationService->send($event,['body' => $message]); // throw error
}
}
这一行有问题:
$this->pushNotificationService = PushNotificationService::class;
当您执行SomeClass::class
时,这意味着您提供了类名,而不是实际的类
因此,当您稍后执行$this->pushNotificationService->send(…)
时,推送通知服务只是类名,而不是服务类
问题的第二部分是,您需要一个实际的对象放入其中。Laravel可以在构造函数中为您注入它,然后您可以提供它。像这样:
public function __construct(PushNotificationService $service)
{
$this->pushNotificationService = $service;
}
这一行的问题在于:
$this->pushNotificationService = PushNotificationService::class;
当您执行SomeClass::class
时,这意味着您提供了类名,而不是实际的类
因此,当您稍后执行$this->pushNotificationService->send(…)
时,推送通知服务只是类名,而不是服务类
问题的第二部分是,您需要一个实际的对象放入其中。Laravel可以在构造函数中为您注入它,然后您可以提供它。像这样:
public function __construct(PushNotificationService $service)
{
$this->pushNotificationService = $service;
}
是的,Joel是对的,请删除::类以使其正常工作。@Rob你能编辑我的代码吗?我在哪里可以更改?@Joel Hinz你好,guyz r u在哪里?@Javed你只需要删除::class
@JoelHinz它返回使用未定义的常量PushNotificationService-假设“PushNotificationService
是的,Joel是对的,请删除::类以使其正常工作。@Rob你能编辑我的代码吗?我在哪里可以更改?@Joel Hinz你好,guyz r u在哪里?@Javed你只需要删除::类@JoelHinz它返回使用未定义的常量PushNotificationService-假定为'PushNotificationService