阅读用JS创建的Laravel Blade中的cookie [英] Read cookies in Laravel Blade created in JS
问题描述
我是初学者Web开发人员.我在Laravel 7和jQuery中创建项目.
I am beginner webdeveloper. I make my project in Laravel 7 and jQuery.
我用JavaScript(jQuery)创建cookie,我需要在Laravel Blade中阅读它.
I create cookies in JavaScript (jQuery) and I need read it in Laravel Blade.
我有此代码:
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self,function(){var r=e.Cookies,n=e.Cookies=t();n.noConflict=function(){return e.Cookies=r,n}}())}(this,function(){"use strict";function e(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)e[n]=r[n]}return e}var t={read:function(e){return e.replace(/%3B/g,";")},write:function(e){return e.replace(/;/g,"%3B")}};return function r(n,i){function o(r,o,u){if("undefined"!=typeof document){"number"==typeof(u=e({},i,u)).expires&&(u.expires=new Date(Date.now()+864e5*u.expires)),u.expires&&(u.expires=u.expires.toUTCString()),r=t.write(r).replace(/=/g,"%3D"),o=n.write(String(o),r);var c="";for(var f in u)u[f]&&(c+="; "+f,!0!==u[f]&&(c+="="+u[f].split(";")[0]));return document.cookie=r+"="+o+c}}return Object.create({set:o,get:function(e){if("undefined"!=typeof document&&(!arguments.length||e)){for(var r=document.cookie?document.cookie.split("; "):[],i={},o=0;o<r.length;o++){var u=r[o].split("="),c=u.slice(1).join("="),f=t.read(u[0]).replace(/%3D/g,"=");if(i[f]=n.read(c,f),e===f)break}return e?i[e]:i}},remove:function(t,r){o(t,"",e({},r,{expires:-1}))},withAttributes:function(t){return r(this.converter,e({},this.attributes,t))},withConverter:function(t){return r(e({},this.converter,t),this.attributes)}},{attributes:{value:Object.freeze(i)},converter:{value:Object.freeze(n)}})}(t,{path:"/"})});
$(document).ready(function () {
$('.cookies-box-close').click(function (e) {
$('.cookies-box').removeClass('d-flex');
$('.cookies-box').addClass('d-none');
Cookies.set('hiddenCookiesSplash', '1', { expires: 30 })
});
});
此代码可正确创建cookie.
This code create cookies correctly.
现在,我尝试在Laravel Blade中检查值:
Now I try check value in Laravel Blade:
@if (Cookie::get('hiddenCookiesSplash') != '1')
Is okey
@endif
但是它不起作用-laravel无法从cookie中读取vale:(
But it's not working - laravel can't read vale from cookie :(
我该如何修理?
推荐答案
此外,您应该记住,默认情况下,Laravel只能读取Laravel设置的cookie.
Also, you should keep in mind that by default Laravel can only read cookies set by Laravel.
Laravel使用Bcrypt自动加密所有cookie.您可以执行以下操作之一:
The Laravel automatically encrypts all cookies with Bcrypt. You can do one of the following:
- 使用Laravel后端创建您的cookie.(推荐)
- 使用Bcrypt加密cookie值,以便Laravel读取它们.
- 禁用Laravel cookie加密,这应允许您的Laravel应用程序从所有来源读取cookie.(不推荐)
来源:
这篇关于阅读用JS创建的Laravel Blade中的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!