代码之家  ›  专栏  ›  技术社区  ›  JahStation

从laravel中的事件刷新视图

  •  0
  • JahStation  · 技术社区  · 6 年前

    我需要等待(听)来自客户端的请求通过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;
    
        }
    

    我怎么能从我的网页应用程序中强制一个页面来反映这个事件对我来说不是那么简单!我应该沿着这条路走还是我离解决方案很远?我很确定我不能从“handle”函数返回到视图。。。致以最诚挚的问候

    0 回复  |  直到 6 年前
    推荐文章