Php 如何在Laravel 5.4中使用服务器发送的事件
我正在尝试使用我的Laravel5.4应用程序做一些简单的sse。我面临的困难实际上来自服务器 这是我的一个控制器方法 信息控制器类Php 如何在Laravel 5.4中使用服务器发送的事件,php,html,laravel,laravel-5.4,Php,Html,Laravel,Laravel 5.4,我正在尝试使用我的Laravel5.4应用程序做一些简单的sse。我面临的困难实际上来自服务器 这是我的一个控制器方法 信息控制器类 public function store(Request $request) { $data = [ 'name' => $request->get('name'), 'country' => $request->get('country') ]; $info = new Info
public function store(Request $request)
{
$data = [
'name' => $request->get('name'),
'country' => $request->get('country')
];
$info = new Information($data); // My model
$info->save();
$push = new PushManager();
$push->readPushValue($info); // intention here is to set the data to be sent as push. This doesnt seem to run.
return response()->json(Information::find($info->id), 00);
}
这是我的PushManager.php类。注意:不是控制器
class PushManager
{
public $data;
public function readPushValue($data)
{
$this->data = data;
}
public function getPush()
{
return $this->data;
}
}
// all neccessary namespaces and use statements
class PushNotification extends Controller
{
private function setupHeaders()
{
// headers for SSE
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('Access-Control-Allow-Origin: *');
}
public function push()
{
$this->setupHeaders();
$push = new PushManager();
$data = $push->getPush(); // suppose to return the value setted in the Information controller
echo "data: {$data}\n\n";
flush();
}
}
最后这是我的推送通知控制器
class PushManager
{
public $data;
public function readPushValue($data)
{
$this->data = data;
}
public function getPush()
{
return $this->data;
}
}
// all neccessary namespaces and use statements
class PushNotification extends Controller
{
private function setupHeaders()
{
// headers for SSE
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('Access-Control-Allow-Origin: *');
}
public function push()
{
$this->setupHeaders();
$push = new PushManager();
$data = $push->getPush(); // suppose to return the value setted in the Information controller
echo "data: {$data}\n\n";
flush();
}
}
我有一个调用PushNotification控制器push方法的路由
现在,每当我的信息控制器(即存储方法)运行$push->readPushValue($data)
时,似乎都不起作用
我知道代码有问题,我希望你能理解我的意图。请帮忙