我需要等待(听)来自客户端的请求通过API路由,这将给我发送一些文本,当收到请求时,我需要确认它给客户端,并发送文本到刀片视图,而不是回到客户端的API。
因此,我开始深入研究事件/侦听器,并写下以下内容:
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
'App\Events\TextAdded' => [
'App\Listeners\AddText',
],
];
然后
<?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 TextAdded
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $testo;
public function __construct($testo)
{
$this->testo=$testo;
}
和
<?php
namespace App\Listeners;
use App\Events\TextAdded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class AddText
{
public function __construct()
{
}
public function handle(TextAdded $event)
{
return view('screen')->with('dato',$event->testo);
}
从api中,我调用了这个测试路由的路由文件:
Route::post('/text', 'ScreenController@refresh');
在控制器上查找此函数:
public function refresh(Request $r){
$txt='aaaaaaaaaaaa';
event(new TextAdded($txt));
return true;
}
我怎么能从我的网页应用程序中强制一个页面来反映这个事件对我来说不是那么简单!我应该沿着这条路走还是我离解决方案很远?我很确定我不能从“handle”函数返回到视图。。。致以最诚挚的问候