您好我正在开发一个使用事件broacasting troug redis和socket.io的Laravel webapp。一切正常,但我要将渲染的视图作为Event的响应返回。 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>
“电源”警报正确显示,但视图也不起作用。在另一个视图中,我将视图用作return response()->json($view)
并且完美地工作......有人有类似的问题吗?
答案 0 :(得分:0)
对于在此问题上挣扎的任何人,通过将视图作为在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
);
}