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