4
我正在進行實時通知,並且偶然發現了這個奇怪的錯誤。我在模型中引入了一個引導方法,該方法觸發了一個名爲SendNotificationData
(無監聽器)的事件。它會在有新的通知時處理。Laravel - 導致404錯誤的事件
試用控制器
<?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();
}
}
通知模型引導方法:
/**
* Handle booting of model.
*
* @var string
*/
public static function boot()
{
static::created(function ($data) {
event(new SendNotificationData($data));
});
parent::boot();
}
這是我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';
}
}
在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>');
}
現在,如果我嘗試在我的控制器中添加一些隨機通知,事件就會觸發。但是,它向我顯示了404錯誤頁面。當我刪除ShouldBroadcast
接口或刪除構造函數的內容時,錯誤不再顯示。我很困惑當我的其他活動正常工作時,會導致這樣的錯誤。我可能錯過了一些東西,請引導我。
你也可以提供你的控制器代碼嗎? – apokryfos
完成添加試用控制器代碼。 –
您的路線設置是否正確,以實際訪問該控制器操作? (函數中的'dd(something)'可以告訴你它是否被命中)。你也可以分享你用來收聽頻道的JS嗎? – apokryfos