I need to waiting (listen) for a request from client trough API route, that will send me some text, when the request is received i need to confirm it to the client and send the text to a blade view not back to the client's api.
I dont think that i can do everything in a controller logic.
So I start to dig into event/listener and I write this:
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\TextAdded' => [
'App\Listeners\AddText',
],
];
then
<?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;
}
and
<?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);
}
and from the api, route file I call for this test route:
Route::post('/text', 'ScreenController@refresh');
thats looking for this function on the controller:
public function refresh(Request $r){
$txt='aaaaaaaaaaaa';
event(new TextAdded($txt));
//tried to send something to view but with no success
return true;
}
Forget about how $txt is made, is just about a test.
How can I force a page from my web app to reflect this event is not so clare to me! Should I follow this road or Im far away from a solution? Im pretty sure that i cannot return to a view from an "handle" function... best regards