您好,我正在开发一个 Laravel 网络应用程序,该应用程序使用事件广播 troug redis 和 socket.io。一切正常,但我试图返回渲染 View 作为事件的响应。 Mi事件是这样的:
<?php
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 EventName implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $data;
public function __construct()
{
$this->data = array(
'power'=> 'Funziona',
'view'=> view('dashboard.partials.messages')->render()
);
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['test-channel'];
}
}
我使用这段代码在页面上呈现响应:
<script src="/frontend/socket.io.js"></script>
<script>
var socket = io('http://1clickfashion.com:3002');
socket.on("test-channel:App\\Events\\EventName", function(message){
// increase the power everytime we load test route
alert(message.data.power);
$('#messages').html('');
$('#messages').html(message.data.view);
});
</script>
“电源”警报显示正确,但 View 不工作。在另一个 View 中,我将 View 用作 return response()->json($view)
并且工作完美......有人有类似的问题吗?
最佳答案
对于在这个问题上挣扎的任何人,通过将 View 作为在 Controller 中呈现的变量传递来解决。像这样。
在 Controller 中:
$view = view('dashboard.partials.messages')->with('post', $post_c)->render();
event(new \App\Events\Post($view));
在事件中:
public $data;
public function __construct($view)
{
$this->data = array(
'view'=> $view
);
}
关于laravel - 渲染 View 作为事件广播的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812754/