在流明5.6中的响应上设置cookie [英] Set cookie on response in lumen 5.6

查看:95
本文介绍了在流明5.6中的响应上设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想这样设置标题

Actually I want to set header like this

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

但标题中未显示 Set-Cookie .有什么办法可以在Lumen 5.6中设置cookie

But the Set-Cookie not shown in the header. Is there any way to set the cookie in Lumen 5.6

我看到了 withCookie(cookie()),但不确定如何使用.流明中的 cookie()未定义.

I saw the withCookie(cookie()) but not sure how to use. The cookie() in Lumen is not defined.

注意:我同时需要2个 set-cookie ,并且 strCookie1 已经是cookie值的完整长字符串.(例如: TOKEN = abcxyz; Path =/; Expires = Sun,2019年3月24日格林尼治标准时间;最大)

Note: I need 2 set-cookie at the same time, and the strCookie1 is already the full long string of the cookie value. (Like this: TOKEN=abcxyz; Path=/; Expires=Sun, 24 Mar 2019 03:40:11 GMT; Max)

谢谢.

推荐答案

根据文档

但是,对于大多数路由和控制器操作,您将返回完整的 Illuminate \ Http \ Response 实例.返回完整的 Response 实例,您可以自定义响应的HTTP状态代码和标头.Response实例继承自 Symfony \ Component \ HttpFoundation \ Response 类,提供了用于构建HTTP响应的多种方法:

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:

您可以在此处.参数是字符串或 Symfony \ Component \ HttpFoundation \Cookie 实例.如果您看到其中的代码(这是链接), string 参数仅在定义了 cookie 函数.在这种情况下,它没有定义.因此,这种方法只给您一个选择:

You can find the appropriate method to set 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), the string argument only accepted when the cookie function is defined. In this case, it's not defined. So this method only leaves you one option:

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

To supply the first argument with an instance of Symfony\Component\HttpFoundation\Cookie

<?php

use Symfony\Component\HttpFoundation\Cookie;

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

这篇关于在流明5.6中的响应上设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆