0

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?

Paul Godard
  • 1,055
  • 1
  • 14
  • 30
  • I don't think you can create a cookie like that. The docs are a bit vague when it comes to them in Laravel... Take a look at https://stackoverflow.com/questions/45207485/how-to-set-and-get-cookie-in-laravel and see if any of the answers there can help with this – Tim Lewis Jul 28 '21 at 18:22
  • I cannot get Response to work. What should I add on top of the controller to use Response? – Paul Godard Jul 28 '21 at 18:28

1 Answers1

4

You must send maked cookie with response.

Cookie::make() just create cookie object on your backed - but, if you not send them to user - he cannot save them.

class TestController extends Controller
{
    public function show($page) {
        //for debug
        if ($cookie = Cookie::get('testcookie')) { dump ($cookie); }
        $cookie = Cookie::make('testcookie','5', 120);
        return response()
            ->view('tests.'.$page,['page' => $page])
            ->withCookie($cookie);
    }
}
Maksim
  • 2,653
  • 2
  • 13
  • 28
  • This makes sense. Of course the cookie can only be saved when the page is viewed by the user as it is stored there. Thank you @Maksim! I am testing it right now. – Paul Godard Jul 28 '21 at 21:52
  • BTW, how do you handle passing 2 different cookies to the view? I mean if the function saved 2 cookies 1 & 2, how do yopu pass them through? simply like ->withCookie($cookie1,$cookie2)? – Paul Godard Jul 29 '21 at 19:55
  • Just try :) or chain method withCookies() – Maksim Jul 29 '21 at 19:56
  • Thank you @Maksim chaining gives an error and ->withCookie($cookie1,$cookie2) does not seem to save cookie2... I can not find anything in the docs – Paul Godard Jul 29 '21 at 20:36
  • Hm, documentation says you can chaining like `->cookie('test1', 5, 120)->cookie('test2', 5 ,120)` – Maksim Jul 29 '21 at 21:32
  • For me it was due to having a dot (.) in my cookie name. It worked in my tests but not in production. – Fredrik Mar 07 '23 at 12:00