如何在Laravel中广播错误和警告?

时间:2018-09-05 09:19:29

标签: php laravel socket.io broadcast phpwebsocket

我正在使用Laravel 5.6和socket.io在应用程序的管理面板中广播消息。

在我的应用程序中,需要广播所有生成的errors and warnings,然后再将其登录到laravel logs files进行管理。

我只是想知道如何实现此功能。自从我刚接触Laravel框架以来,还有哪些简单的方法。

这是我到目前为止所做的:

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ErrorBroadcasting implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

    public $errorContent;

    public function __construct($errorContent)
    {
        $this->errorContent = $errorContent;
    }

    public function broadcastOn()
    {
        return new PresenceChannel('error-broadcasting-channel');
    }
}

这就是我在刀片中使用通道的方式。

<script>

        window.Echo = new Echo({
            broadcaster: 'socket.io',
            host: window.location.hostname + ':6001',
        });

        var buyer = Echo.channel(`error-broadcasting-channel`);
        buyer.listen("ErrorBroadcasting", e => {
            $('#error_container').append("<div class=\"row chat-snippet\">\n" +
                "                                        <div class=\"col-lg-12\">\n" +
                "                                            <small>Buyer:</small>\n" +
                "                                            <p>"+e.errorContent+"</p>\n" +
                "                                        </div>\n" +
                "                                    </div>");
        });
</script>

这就是我修改app \ Exceptions \ Handler.php

的方式
public function report(Exception $exception)
    {
        if (config('app.mail_exception') && $this->shouldReport($exception)) {
            $this->sendEmail($exception); // sends an email
        }
        broadcast(new ErrorBroadcasting($exception))->toOthers();
        parent::report($exception);
    }

1 个答案:

答案 0 :(得分:1)

在您的

  

app / Exceptions / Handler.php

在此类中,您可以在render方法中

 public function report(Exception $exception)
    {

        broadcast($yourEvent)
        parent::report($exception);
    }