【问题标题】:Laravel 419 page expire error at /broadcasting/auth, but csrfToken is added LARAVEL NOTIFICATIONS/broadcasting/auth 出现 Laravel 419 页面过期错误,但添加了 csrf Token LARAVEL NOTIFICATIONS
【发布时间】:2021-02-19 21:26:01
【问题描述】:

Laravel 7

我正在尝试在我的项目中添加 Laravel 通知,但一直在收听通知,主要是在广播/身份验证时出现错误我已经解决了这个问题一次但仍然没有收到通知是我的代码

App\Notifications\MessageReceived

class MessageReceived extends Notification
{

    use Queueable;



    /**
     * Create a new notification instance.
 *
 * @return void
 *
 */

public $notification;

public function __construct($notify)
{
    //
    $this->notification=$notify;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['database'];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
                ->line('The introduction to the notification.')
                ->action('Notification Action', url('/'))
                ->line('Thank you for using our application!');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */


public  function toDatabase($notifiable){

    return [
        'MessageSender'=>$this->notification,
    ];
}



public function toArray($notifiable)
{
    return [
        'MessageSender'=>$this->notification,
    ];
}

public function toBroadcast($notifiable)
{
    return new BroadcastMessage([
        'MessageSender'=>$this->notification,
    ]);
}

我的定制警卫。管理模型

    class Admin extends Authenticatable
    {
        use Notifiable;
    
        protected $guard = 'admin';
        protected $fillable = [
            'name', 'email', 'password',
        ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function receivesBroadcastNotificationsOn()
    {

        return 'admin.'.$this->id;
    }
}

路由/channel.php

Broadcast::channel('App.User.{id}', function ($user, $id) {

    return (int) $user->id === (int) $id;
});

Broadcast::channel('App.Admin.{id}', function ($admin, $id) {

    return (int) $admin->id === (int) $id;
},['guards' => ['admin']]);

public/js/listner.js(我在 js/app.js 文件的链接下方使用这个 js 文件链接,我已经对其工作进行了测试)

Echo.private('App.Admin.${id}' )
    .notification((notification) =>{
        window.alert(notification.type)
        console.log(notification)
    });

触发通知的控制器方法

 public function store(Request $request)
    {
        //

        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required',
            'message' => 'required'
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }
        
        $admin = Admin::find(1);
       $admin->notify(new MessageReceived($request->email));


        return response()->json([
            'success'=>'Message sent successfully. we will get to you soon as possible',
//            'data'=>$noty,
        ], 200);
    }

资源/js/boostrap.js

 import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true,
    authEndpoint: "/broadcasting/auth",
    auth: {
        headers: {
            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                },
                },
    // csrfToken:document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
});
window.Pusher.log = function(message ,err){
  console.log(message);
  console.log(err)
}

这是我的所有代码,但我仍然无法收听触发的通知, 我的 env 文件具有正确的所有值,例如 BROADCAST_DRIVER=pusher 和 pusher keys 等

需要一些关于我错在哪里以及我应该怎么做才能完成这项工作的指南

【问题讨论】:

  • 我也取消了广播服务提供者的注释

标签: php laravel laravel-7 pusher laravel-echo


【解决方案1】:

Notification 类中的 via 方法应该返回 ['broadcast']

同时从 Notifications 类中移除 use Queueable; trait。

不要忘记在 Pusher 初始化器中添加 namespace 属性为 App.Events

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true,
    authEndpoint: "/broadcasting/auth",
    namespace: 'App.Events'
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2020-06-02
    • 2023-01-18
    • 2019-08-15
    • 2020-05-22
    • 2022-06-11
    • 2017-06-03
    相关资源
    最近更新 更多