2017-09-24 2 views
4

Je suis en train de faire des notifications en temps réel et trébuché dans cette erreur bizarre. J'ai dans mon modèle une méthode de démarrage qui déclenche un événement appelé SendNotificationData (pas d'écouteur). Il gère quand il y a une nouvelle notification faite.Laravel - événement provoquant l'erreur 404

Contrôleur d'essai

<?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(); 
    } 
} 

méthode de démarrage du modèle de notification:

/** 
* Handle booting of model. 
* 
* @var string 
*/ 
public static function boot() 
{ 
    static::created(function ($data) { 
     event(new SendNotificationData($data)); 
    }); 

    parent::boot(); 
} 

Ceci est mon événement 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'; 
    } 
} 

Sur 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>'); 
} 

Maintenant, si j'essaie de tester l'ajout d'une notification aléatoire dans mon contrôleur, l'événement se déclenche. Cependant, il me montre la page d'erreur 404. Lorsque j'ai supprimé l'interface ShouldBroadcast ou supprimer le contenu du constructeur, l'erreur n'apparaît plus. Je suis confus ce qui causerait une telle erreur quand mes autres événements fonctionnent bien. J'ai peut-être manqué quelque chose, alors guide-moi s'il te plaît.

+0

Pouvez-vous également fournir votre code de contrôleur? – apokryfos

+0

Terminé en ajoutant le code du contrôleur d'essai. –

+0

Votre route est-elle correctement configurée pour visiter cette action du contrôleur? (Un 'dd (quelque chose)' dans la fonction pourrait vous dire si elle est touchée ou non). Pouvez-vous également partager le JS que vous utilisez pour écouter sur la chaîne? – apokryfos

Répondre

1

Je ne peux pas le croire, il a été causé par la variable $incrementing dans le modèle étant réglé sur false au lieu de true. Si seulement laravel me montrerait la trace de la pile d'erreur appropriée.