从Laravel中的事件刷新视图

时间:2018-09-10 16:05:25

标签: laravel laravel-blade laravel-events

我需要等待(侦听)来自客户端通过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的制作方式,只是测试。

如何强制我的Web应用程序中的页面来反映此事件对我而言并不那么有趣!我应该走这条路还是我远离解决方案?我非常确定我无法从“句柄”功能返回视图...最好的问候

0 个答案:

没有答案