1

I tried to set cookie (easy example):

Route::get('set', function () {
    Cookie::queue(Cookie::forever('cart_id', 123));
});

Route::get('get', function (Illuminate\Http\Request $request) {
    dump($request);

});

But I get in dump of $request:

Request {#40 ▼
  ...
  +cookies: ParameterBag {#43 ▼
    #parameters: array:3 [▼
      "cart_id" => null
      "XSRF-TOKEN" => null
      "laravel_session" => null
    ]
  }
  +headers: HeaderBag {#46 ▼
    #headers: array:9 [▼
      ...
      "cookie" => array:1 [▼
        0 => "cart_id=eyJpdiI6IldNbjJwMHdRclBmWkFkUFJ3SnJ6Z2c9PSIsInZhbHVlIjoiVnhBWGl3ODFBaGhwTCtSc0NoSmlLUT09IiwibWFjIjoiYjU2NjRmNDI4OGQ4NGZmZjNjMDhmYTEwMWJlOWQ2ZjUwYjAxNTMxYjU2OWY3Y2I0NjYxNzQzZTNhZjVkMDcxOSJ9; XSRF-TOKEN=eyJpdiI6Ino2ckNVSU5cL3VEcE5VQWRqQm1wTkV3PT0iLCJ2YWx1ZSI6IkF2b1Y0VDFkMlVqam8zeWVJQk55Mm96XC9PZFlyZUxma3FBTXNBV2VoK3AyTkp5Q2RNOHZtQnRyMFB0a3c0Mm5cL1hJdkkyY1haUHhZV0FEejdKSjMzaHc9PSIsIm1hYyI6IjFlNjVkZDE2NDJiYjA2MDQzM2JiYWYzMWY2YWRiNTk0ZDM4MjBlNmEzZmEwNTUxZWUzY2NmYmEyZDljMmYwOGIifQ%3D%3D; laravel_session=eyJpdiI6Imh1QW1iamlscnVpV3JFTThHYWdTNkE9PSIsInZhbHVlIjoiZElUMU1MZlRwa1BRZFd6T0pWMGhSanNCcmVNcjdEUGJrd2Zhc3RqbFUzVFBOamxsclBnbWhHcVk4QXFXU2VFMkVGYXBUSm4xVVFvcGZoZnVxN1VTVnc9PSIsIm1hYyI6IjkyYjA5YTI3MmE3MWE5OWRhNDBjYWZkZjQwZjk0NjI1OTY4MGNjZDM4ZDQ4YWE3YjdkMWMxNmQwZjgxMzQzZTYifQ%3D%3D"
      ]
    ]
    #cacheControl: array:1 [▶]
  }      
}

Of course, firstly I go to /set link, then /get.

Where did I go wrong? Why all of my cookies equals to null?

By the way, last 5 hours I tried to make a function, that will contains Cookie::queue() function, but it goes wrong too. I dont know, what's wrong with my laravel cookies

Alexxosipov
  • 1,215
  • 4
  • 19
  • 45
  • What version exactly do you use? Run `php artisan -V` – Alexey Mezenin Jan 18 '18 at 20:03
  • @AlexeyMezenin 5.2.45 – Alexxosipov Jan 18 '18 at 20:11
  • Did you apply `web` middleware to the routes? If you did, remove it. Your code works perfectly in 5.3 and 5.5. – Alexey Mezenin Jan 18 '18 at 20:13
  • @AlexeyMezenin yes, thanks! But...as I know, web middleware contains encrypting cookies and csrf protection, what should I do with that? And also `AddQueuedCookiesToResponse` middleware, what if I need to use this middlewares? – Alexxosipov Jan 18 '18 at 20:18
  • The thing is [since 5.2.27 Laravel applies this middleware to all routes](https://stackoverflow.com/a/36784408/1227923) that are in `routes/web.php`, so just do not apply this middleware manually and everything will work as expected. Click the link, read the answer and If it was helpful, please upvote it. – Alexey Mezenin Jan 18 '18 at 20:20
  • @AlexeyMezenin oh, thank you! Voted :) – Alexxosipov Jan 18 '18 at 20:33

0 Answers0