mercredi 28 juillet 2021

cookie::make does not save the cookie in Laravel 8

Am I missing something? I am pulling my hair to solve this simple use of cookie. My intention is simply to save a variable for each user (I tried session and there were side effect issues). The below code should theorically save the cookie which should be there at the next call of the page, correct? It does not work. What am I missing?

class TestController extends Controller
  {
  public function show($page) {
    echo @Cookie::get('testcookie');
    if (Cookie::get('testcookie')) { echo 'cookie exists<br>'; } 
    else { echo 'cookie does not exist<br>'; }
    $cookie = Cookie::make('testcookie','5', 120);
    echo $cookie;
    return view('tests.'.$page,['page' => $page]);
  } 
}

I have also modified config/session.php as recommended for use on http://localhost. Should I clean/cache... or similar after that. I am using laravel 8 & FF on OSX.

'secure' => env('SESSION_SECURE_COOKIE', false)

Can someone please tell me what I am doing wrong?

If I try the other way with response...

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

public function show($page, Request $request)
{
    echo $request->cookie('name');

    if ($request->cookie('name')) { echo 'cookie exists<br>'; } else { echo 'cookie does not exist<br>'; }

    $response = new Response('Set Cookie');

    $cookie = $response->withCookie(cookie('testcookie','5', 120));

    echo $cookie;

    return view('tests.'.$page,[
        'page' => $page
    ]);
} 

I get an error "Call to undefined method Illuminate\Support\Facades\Response::withCookie() ".

Why is it so complicated and so simple in php?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3BSQzti
via IFTTT

Aucun commentaire:

Enregistrer un commentaire