0

我需要等待(监听)来自客户端通过 API 路由的请求,这将向我发送一些文本,当收到请求时,我需要向客户端确认并将文本发送到刀片视图而不是返回客户端的 api .

我不认为我可以在控制器逻辑中做所有事情。

所以我开始深入研究事件/侦听器并写下这个:

    class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    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;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    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
    {
        /**
         * Create the event listener.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }

        /**
         * Handle the event.
         *
         * @param  TextAdded  $event
         * @return void
         */
        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));
//tried to send something to view but with no success


        return true;

    }

忘记 $txt 是如何制作的,只是一个测试。

如何强制我的网络应用程序中的页面反映此事件对我来说不是那么清楚!我应该走这条路还是远离解决方案?我很确定我不能从“句柄”功能返回到视图......最好的问候

4

0 回答 0