Laravel - 导致404错误的事件

时间:2017-09-24 07:15:33

标签: php laravel-5.2

我正在制作实时通知,并偶然发现了这个奇怪的错误。我在我的模型中有一个引导方法,它触发一个名为SendNotificationData的事件(没有监听器)。它会在有新通知时处理。

试用控制器

<?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接口或删除构造函数的内容时,错误不再显示。当我的其他事件正常工作时,我很困惑会导致这样的错误。我可能错过了什么,所以请指导我。

1 个答案:

答案 0 :(得分:3)

我无法相信,这是由模型中的$incrementing变量设置为false而不是true引起的。如果只有laravel会显示正确的错误堆栈跟踪。