在 lumen 5.6 中设置 cookie 响应

Set cookie on response in lumen 5.6

其实我想这样设置页眉

response()->json($data)->header('Set-Cookie','strCookie1',false)->header('Set-Cookie','strCookie2',false)->send();

Set-Cookie 未显示在页眉中。 有什么办法可以在 Lumen 5.6

中设置 cookie

我看到了 withCookie(cookie()) 但不知道如何使用。 Lumen中的cookie()未定义

注意:我同时需要2个set-cookiestrCookie1已经是cookie值的完整长串了。 (像这样:TOKEN=abcxyz; Path=/; Expires=Sun, 24 Mar 2019 03:40:11 GMT; Max

谢谢。

根据文档here

However, for most routes and controller actions, you will be returning a full Illuminate\Http\Response instance. Returning a full Response instance allows you to customize the response's HTTP status code and headers. A Response instance inherits from the Symfony\Component\HttpFoundation\Response class, providing a variety of methods for building HTTP responses:

您可以找到合适的方法来设置 cookie here. The argument is either string or a Symfony\Component\HttpFoundation\Cookie instance. If you see the code inside it (here's the link),string 参数仅在 cookie 函数已定义。在这种情况下,它没有定义。所以这个方法只给你一个选择:

为第一个参数提供一个实例 Symfony\Component\HttpFoundation\Cookie

<?php

use Symfony\Component\HttpFoundation\Cookie;

$response
    ->withCookie(
        new Cookie($name, $value, $expire)
    );