我正在制作实时通知,并偶然发现这个奇怪的错误.我在我的模型中有一个启动方法,它触发一个名为SendNotificationData(no listener)的事件.它会在有新通知时处理.
试用控制器
<?php
namespace App\Http\Controllers\Notification;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Notification;
class NotificationController extends Controller
{
/**
* Trigger event to display notifications. This displays 404 error page
*
* @return none
*/
public function displayNotification()
{
$notification = new Notification();
$notification->EmployeeID = "EMP-00001";
$notification->NotificationText = "There is a new notification";
$notification->NotificationStatus = "unread";
$notification->NotificationType = "trial";
$notification->save();
}
}
Run Code Online (Sandbox Code Playgroud)
通知模型启动方法:
/**
* Handle booting of model.
*
* @var string
*/
public static function boot()
{
static::created(function ($data) {
event(new SendNotificationData($data));
});
parent::boot();
}
Run Code Online (Sandbox Code Playgroud)
这是我的SendNotificationData活动:
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class SendNotificationData extends Event implements ShouldBroadcast
{
use SerializesModels;
public $new_notification_data;
/**
* Create a new event instance.
*
* @param $notification_data
* @return void
*/
public function __construct($new_notification_data)
{
$this->new_notification_data = $new_notification_data;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['new-notification'];
}
/**
* Customize event name.
*
* @return array
*/
public function broadcastAs()
{
return 'private-send-new-notification';
}
}
Run Code Online (Sandbox Code Playgroud)
上 Javascript
var newNotificationChannel = pusher.subscribe('new-notification');
newNotificationChannel.bind("private-send-new-notification", function(data) {
addNotification(data);
}); //This gives me no error in the console and the 404 error still shows up even if i remove this..
function addNotification(data)
{
console.log(data);
$('.notification-link').closest('li').append('<a href="#">This is a sample notification!!!</a>');
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试在我的控制器中测试添加一些随机通知,则会触发该事件.但是,它显示了404错误页面.当我删除ShouldBroadcast接口或删除构造函数的内容时,错误不再显示.当我的其他事件正常工作时,我很困惑会导致这样的错误.我可能错过了一些东西,所以请指导我.