Home > Software design >  Laravel: Call controller with Request::create using POST-method and prevent session from expiring
Laravel: Call controller with Request::create using POST-method and prevent session from expiring

Time:09-01

Sorry if it doesn't translate well, I use the google translator, I have the following controller:

public function call($ruta,$parametros,$cookies) {

// create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], $content = null)

$req = Request::create($ruta,"post",$parametros,$cookies);
$res = app()->handle($req);
$respu = $res->getContent();

return $respu;}

I call this controller from my websocket server to call the internal functions that I have in my webapp, and I call the function like this:

$respu = $callController->call($datos['ruta'],['XSRF-TOKEN'=>$datos['_token']],['_token'=>$datos['_token'],'XSRF-TOKEN'=>$datos['XSRF-TOKEN'],'suplifalcon_session'=>$datos['suplifalcon_session']]);

and I receive as a response a page indicating that it has expired

I have tried to send all the cookies and the csrf to the websocket server and call the method, but nothing works, does anyone know the solution? Example of what I have tried:

<meta name="csrf-token" content="{{ csrf_token() }}">
<meta name="cooki1" content="{{ $_COOKIE['XSRF-TOKEN'] }}">

<meta name="cooki2" content="{{ $_COOKIE['suplifalcon_session'] }}">

CodePudding user response:

in laravel post http reqest under web middleware needs to verified by a token. laravel provides two ways to send this token to server:

  • a parameter named _token that is included in form
  • header named X-CSRF-TOKEN

the value of both is a token that is generated by csrf_token() helper.

are you sure token created properly ?

check token in variable $datos['_token']

  • Related