laravelÖÐÉèÖÃcookieÓò
ÈôÊÇÄúÕýÔÚʹÓà laravel ¿ò¼Ü¿ª·¢ÍøÕ¾Ó¦ÓóÌÐò£¬Äú¿ÉÄÜÐèÒªÉèÖà cookie ÓòÀ´È·±£ÄúµÄ cookie ÊÊÓÃÓÚÖ¸¶¨µÄÓòÃû¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùÔÚ laravel ÖÐÉèÖà cookie Óò¡£
ʲôÊÇ Cookie Óò
Cookie ÓòÊÇÖ¸ Cookie ¿ÉÓõÄÓòÃû»ò×ÓÓòÃûÁÐ±í¡£ÀýÈ磬ÈôÊÇÄúÉèÖÃÁËÊÊÓÃÓÚ “.example.com” ÓòµÄ Cookie£¬ÄÇôÕâ¸ö Cookie ¿ÉÒÔÔÚ example.com µÄÈκÎ×ÓÓòÃûÏÂʹÓã¬ÀýÈç blog.example.com ºÍ shop.example.com¡£ÈôÊÇÄúÉèÖÃµÄ Cookie ÓòΪ “example.com”£¬ÔòÕâ¸ö Cookie Ö»ÄÜÔÚ example.com µÄ¸ùÓòÃûÏÂʹÓá£
ÔÚ Laravel ÖÐÉèÖÃ Cookie Óò
ÔÚ Laravel ÖУ¬Äú¿ÉÒÔʹÓà cookie È«¾ÖÖúÊÖºÍ withCookie ÒªÁìÀ´ÉèÖÃÓ¦ÓóÌÐòµÄ Cookie Óò¡£
ÒªÉèÖà Cookie Óò£¬ÇëʹÓà cookie È«¾ÖÖúÊÖÉèÖÃÒ»¸öÐ嵀 Cookie£¬°üÀ¨ÄúҪʹÓõÄÓòÃû£º
$response = response('Hello World')->cookie( 'name', 'value', $minutes, '/', '.example.com', false, true );
µÇ¼ºó¸´ÖÆ
´Ë´úÂ뽫ÔÚ “.example.com” ÓòÏÂÉèÖÃÒ»¸öÃûΪ “name” ֵΪ “value”£¬ÓâÆÚʱ¼äΪ $minutes ·ÖÖ Cookie¡£
Äú¿ÉÒÔʹÓà withCookie ÒªÁ콫ÉÏÊö´úÂëÓ¦ÓÃÓÚÓ¦ÓóÌÐòµÄÏìÓ¦ÖУº
return response('Hello World')->withCookie($cookie);
µÇ¼ºó¸´ÖÆ
Çë×¢ÖØ£¬withCookie ÒªÁìÒ²¿ÉÒÔÒ»´ÎÉèÖöà¸ö Cookie£º
return response('Hello World')->withCookie($cookie1, $cookie2, $cookie3);
µÇ¼ºó¸´ÖÆ
ʾÀý
$cookie = cookie('name', 'value', 60); return response('Hello World')->withCookie($cookie);
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂ뽫ÔÚÄ¿½ñÓòÃûÏÂÉèÖÃÒ»¸öÃûΪ “name” ֵΪ “value”£¬ÓâÆÚʱ¼äΪ 60 ·ÖÖ Cookie¡£
ÈôÊÇÄúÏ뽫 Cookie ÓòÏÞÖÆÔÚÄ¿½ñ¸ùÓòÃûÏ£¬Äú¿ÉÒÔʹÓãº
$response = response('Hello World')->cookie( 'name', 'value', $minutes, '/' );
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚÄ¿½ñÓòÃûµÄ¸ùĿ¼ÏÂÉèÖÃÒ»¸ö Cookie¡£
×ܽá
ÔÚ Laravel ÖÐÉèÖà Cookie ÓòÐèÒª½ç˵ҪʹÓõÄÓòÃûºÍ×ÓÓòÃûÁÐ±í¡£Äú¿ÉÒÔʹÓà cookie Óë withCookie ÒªÁìÀ´ÇáËɵØÉèÖÃÓ¦ÓóÌÐòµÄ Cookie£¬²¢ÔÚÐèҪʱÉèÖà Cookie Óò¡£Ï£Íû±¾ÎÄÄܹ»×ÊÖúÄú¸üºÃµÄÖÎÀí Laravel ÖÐµÄ Cookie Óò¡£
ÒÔÉϾÍÊÇlaravelÖÐÉèÖÃcookieÓòµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡