如何在 Laravel 5.2 中使用 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36150022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to work with cookies in Laravel 5.2
提问by Akshay Vaghasiya
I'm setting cookie on some click event. Then after storing value in cookie, I want to
我在一些点击事件上设置 cookie。然后在cookie中存储值后,我想
- Check for existence of cookie
- Get cookie values
- 检查cookie是否存在
- 获取 cookie 值
I have developed a function by referring Laravel official docs. Console shows that cookies have been set. But after that, I can not solve two point (mentioned in above list) for view(Blade Template). It always shows (Cookie::get('cookie.clients'))
'null'. But browser console displays that cookie
.
If anyone knows answer, it will be appreciated.
我通过参考 Laravel 官方文档开发了一个功能。控制台显示 cookie 已设置。但是在那之后,我无法解决视图(刀片模板)的两点(在上面的列表中提到)。它总是显示(Cookie::get('cookie.clients'))
“空”。但是浏览器控制台会显示那个 cookie 。如果有人知道答案,将不胜感激。
Here is my code.
这是我的代码。
Controller
控制器
use App\Client;
use App\Http\Requests;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Repositories\ClientRepository;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
class ClientController extends Controller
{
public function cookieadd(Request $request, Client $client)
{
$clients = [];
if (Cookie::get('cookie.clients') != null)
{
$clients = Cookie::get('cookie.clients');
}
array_push($clients, $client);
Cookie::forever('cookie.clients', $clients);
return redirect('/client');
}
}
View
看法
@if (Cookie::get('cookie.clients') != null)
<p>cookie is set</p>
@else
<p>cookie isn't set</p>
@endif
回答by Ben Swinburne
You're creating a cookie object but you're not sending it with the response.
您正在创建一个 cookie 对象,但您没有将它与响应一起发送。
You can either, add it directly to your response in a controller
您可以将其直接添加到控制器中的响应中
$cookie = Cookie::forever('cookie.clients', $clients);
return redirect('/client')->withCookie($cookie);
Or you can queue a cookie and use the AddQueuedCookiesToResponseMiddleware to automatically add it to the Response.
或者您可以将 cookie 排队并使用AddQueuedCookiesToResponse中间件自动将其添加到响应中。
Cookie::queue(Cookie::forever('cookie.clients', $clients));
return redirect('/client');
回答by D0mski
Here's another perspective.
这是另一个视角。
If I understood correctly you've used Javascript to set the cookie on the client side, and you want to pick it up on the server side.
如果我理解正确,您已经使用 Javascript 在客户端设置了 cookie,并且您想在服务器端获取它。
Laravel encrypts its cookies and when you use $request->cookies()
you get the decrypted values, and retrieving the cookies from client side doesn't pass the decryption process and we get null
s. I guess, I haven't been able to dig out the appropriate source code :)
Laravel 加密它的 cookie,当你使用时你$request->cookies()
会得到解密的值,从客户端检索 cookie 不会通过解密过程,我们得到null
s。我想,我一直无法挖掘出适当的源代码:)
You can still use the raw PHP $_COOKIE['cookie_clients']
to retrieve the cookie set by client. Notice, .
is converted to _
by PHP (as seen on php.net).
您仍然可以使用原始 PHP$_COOKIE['cookie_clients']
来检索客户端设置的 cookie。注意,由 PHP.
转换为_
(如 php.net 所示)。
Alternatively, you could add the name of the cookie of interest into $except
in Http/Middleware/EncryptCookies.php
, which will allow you to pick it up with the more Laravel approach of $request->cookie()
.
或者,您可以将感兴趣的 cookie 的名称添加到$except
in 中Http/Middleware/EncryptCookies.php
,这将允许您使用$request->cookie()
.
I'm sure there are many other ways of going around Laravel here, hopefully this gives you an idea of where to look if you don't like either approach. Good luck.
我相信这里有很多其他的方法可以绕过 Laravel,希望这能让你知道如果你不喜欢任何一种方法,应该去哪里看看。祝你好运。
回答by awar33
Did you try to display the cookie via the Request object ?
您是否尝试通过请求对象显示 cookie?
$value = $request->cookie('name');
See laravel doc for more details : https://laravel.com/docs/5.2/requests#cookies
有关更多详细信息,请参阅 laravel 文档:https://laravel.com/docs/5.2/requests#cookies