½ñÌiÔÚLaravel¿o¼ÜÖÐʹÓÃCookieµÄʱºo£¬Åoµ½Á˵aÎÊÌa£¬×Ô¼º±»ÃÔºýÕÛÌÚÁË°e¶aСʱ.ÆÚ¼aÑо¿ÁËCookieµÄʵÏÖÀa£¬Ò²ÔÚÍøÕ¾ÕÒÁËÐi¶aµÄ×ÊÁÏ£¬°uÀ¨ÎÊ´ð¡£¢ÏÖ²¢Ã»Óнa¾oÎÊÌa¡£ÍøÉϵĴ𰸶¼ÊÇ»¥Ïa³Ï®£¬»¥ÏaתÔØ¡£Æaʵ²¢Ã»ÓÐʲôÓô¦¡£ºÃÔÚ×iºo£¬ÎÒÕÒµ½Á˾o¾ø½¨¡£±¼×ÅΪ¹a´oLaravel°®ºÃÕߺͿª¢ÈËÔ±¸ºÔðµÄ¾«Éñ£¬Í¬Ê±Ò²Ï£Íu´o¼ÒÔÚʹÓÃCookieʱÉÙ×ßÍa£¬ÔÚÕaÀi°ÑÔÚLaravelÖÐCookieµÄÉeÖúͶÁÈ¡½*¨¹±Ï׳oÀ´£¬¹(C)´o¼ÒÅuÆÀÖ¸Õý¡£
¸ÅÊo
CookieµÄÌi¼ÓÆaʵºÜ¼oµ¥£¬Ö±½ÓʹÓÃCookie::make()£¬ÔÚʹÓý¨Ç°£¬ÐeÒªÒýÈeCookieµÄÃÅÃaeuse Illuminate\Support\Facades\Cookie;£¬ÕaÑu¾Í¿ÉÒÔÍe³ÉCookieµÄÉeÖÃÁË(µ±È»²»ÒýÈeÖ±½Ó \Cookie ʹÓÃÒ²ÄÜͨ¹ýÃuÃu¿Õ¼a×Ô¶¯¼ÓÔØ)¡£
²»¹ý£¬ÉeÖúoÎÒÃÇÈçºÎ²ÅÄÜÈ¡µ½CookieÖµÄØ£¿ÓÐËÑË÷¹ýÏa¹ØÎÊÌaµÄ¿ª*¢ÈËÔ±¿Ï¶¨ÖªµÀ£¬ÍøÉϵĴð°¸Ç§ÆªÒ»Âɶ¼ÊÇ£ºCookie::get()£¬ÓеÄÉoÖÁ¸½ÉÏÁË´uÂe£º
Cookie::make('test', 'hello, world', 10);
echo Cookie::get('test');
Èç¹uÄa°´ÕÕÕaÖÖÀaËƵĻشðÀ´²aÊÔCookie£¬¿Ï¶¨»a*¢ÏÖÉeÖÃcookieµÄÖµÓÀÔ¶¶¼ÊÇnull¡£¶a´Î²aÊÔÎÞЧÏ£¬Äa¶¼»a»³ÒÉ×Ô¼ºµÄLaravel¿o¼ÜÊDz»ÊÇÓÐÎÊÌa£¡
Æaʵ£¬ÔÚLaravel¿o¼ÜÖÐʹÓÃCookie£¬¾Í²»µÃ²»Ìaµ½ResponseºÍRequest¡£¾³£ÓÃa¯ÀÀÆ÷µ÷ÊÔ³ÌÐoµÄ¿ª¢Õß¿ÉÄÜ»aÁoÒa¹ý£¬ÔÚÇeÇoµØÖµÄResponse HeadersºÍRequest HeadersÖУ¬¶¼°uº¬ÓÐcookieÐÅÏ¢¡£Ã»´i£¬Èç¹uÄaÔÚLaravel¿o¼ÜÖÐʹÓÃCookie¶¼Àe²»¿ªResponseºÍRequest£¬ÏÂÃaeÎÒÃǾÍÀ´½eÉÜCookieµÄÕýÈÌi¼ÓºÍ»ñÈ¡½*¨¡£
Cookie::make()¡¢Cookie::forever()¡¢Cookie::get()µÄʹÓý¨£º
Route::get('cookieset', function()
{
$foreverCookie = Cookie::forever('forever', 'Success');
$tempCookie = Cookie::make('temporary', 'Victory', 5);//²ÎÊý¸ñʽ£º$name, $value, $minutes
return Response::make()->withCookie($foreverCookie)->withCookie($tempCookie);
});
Route::get('cookietest', function()
{
$forever = Cookie::get('forever');
$temporary = Cookie::get('temporary');
return View::make('cookietest', array('forever' => $forever, 'temporary' => $temporary, 'variableTest' => 'works'));
});
//ÏÈÉÏÒ»¸odemoдÈecookie
$cookie = \Cookie('cookie_name', 'value', 5);
$data = ['title'=>'hello world'];
return \response()
->view('home.hello', $data)
->cookie($cookie);
2¡¢Cookie´ae´¢Êý×e£º
Route::get('cookieset', function()
{
$user_info = array('name'=>'laravel','age'=>12);
$user = Cookie::make('user',$user_info,30);
return Response::make()->withCookie($user);
});
Route::get('cookietest', function()
{
dd(Cookie::get('user'));
});
ÏÂÃaeÏeϸ̸̸laravelµÄcookie¾ßÌaÕ¦Íae°É
Ìi¼ÓCookie
ÀýÈ磬ÎÒÃÇÐeÒªÔÚ¿ØÖÆÆ÷ÖÐÉeÖÃÒ»¸o"Hello, Laravel"µÄcookieÖµ£¬²¢ÉeÖÃÓÐЧÆÚΪ10ÖÖÓ¡£ÕaÀiÍƼoʹÓÃcookieµÄ¶ÓÁн*¨Cookie::queue()£¬ÒoΪÕaÑuCookie»a×Ô¶¯Ìi¼Óµ½ÏiÓ¦£º
<?php
namespace App\Http\Controllers;
use Cookie;
use App\Http\Controllers\Controller;
class DashboardController extends Controller
{
/**
* Show the application index.
*
* @return Response
*/
public function index()
{
Cookie::queue('test', 'Hello, Laravel', 10);//Èç¹u²»ÊÊÓÃÉÏÃaeµÄuse Cookie,ÕaÀi¿ÉÒÔÖ±½Óµ÷Óà \Cookie
return view('index');
}
}
¿´¿´Response HeadersÖÐÊDz»ÊǶaÁËÒ»Ìoset-cookie¼Ç¼¡£µ±È»£¬Èç¹uÄaʹÓõÄÊÇResponse£¬¿ÉÒÔÖ±½ÓʹÓÃResponseÖеÄwithCookie()½¨£¬½«cookieÌi¼Óµ½ÏiÓ¦ÖУº
public function index()
{
//$response = new Response();
$cookie = Cookie::make('test', 'Hello, Laravel', 10);
¡¡¡¡ return \Response::make('index')->withCookie($cookie);
//return $response->make('index')->withCookie($cookie);
}
ÒªÉeÖÃÒ»¸oÓÀ²»¹ýÆڵĵÄcookieÖµ£¬¿ÉÒÔʹÓÃCookie::forever()½¨£º
Cookie::forever('test', 'Hello, Laravel');
Cookie×ÔÉi²¢²»Ìa¹(C)¸Ã½¨£¬ÓÉÓÚCookieµÄÃÅÃaeÓÉ\Illuminate\Cookie\CookieJarÌa¹(C)£¬ËuÒÔCookie¿ÉÒÔʹÓøÃÀaÖеĽ¨¡£ÕaÀi¸½ÉÏqueue()½¨µÄÔ´Âe£º
/**
* Queue a cookie to send with the next response.
*
* @param mixed
* @return void
*/
public function queue()
{
if (head(func_get_args()) instanceof Cookie) {
$cookie = head(func_get_args());
} else {
$cookie = call_user_func_array([$this, 'make'], func_get_args());
}
$this->queued[$cookie->getName()] = $cookie;
}
´ÓÔ´Âe¿ÉÒÔµÃÖª£¬queue()½¨Êµ¼ÊÉÏÒ²Êǵ÷ÓõÄmake()½¨¡£
×¢Òa£ºÓÐÐ(C)Ë®ÓÑÌa³oÔÚµ»ØµÄÊÓͼÉÏ×¢ÈecookieµÄ½*¨return view('index')->withCookie($cookie)
£¬ÇײaÎÞЧ¡£ÍƼoʹÓà queue()
»ñÈ¡Cookie
ÔÚ¸ÅÊoÀiÎÒÃÇÌaµ½¹ý£¬CookieµÄʹÓÃÀe²»¿ªResponseºÍRequest¡£»ñÈ¡CookieµÄÖµÓÐÁ½¸o²aÃae£¬Ò»¸oÊÇþÎñ¶Ë£¬ÁiÒ»¸oÊÇ¿Í»§¶Ë¡£Èç¹uÒªþÎñ¶Ë»ñÈ¡µ½CookieµÄÖµ£¬¾ÍÐeÒª´ÓRequestÖлñµÃ£º
public function index(Request $request)
{
$cookie = $request->cookie('test');
dump($cookie);
}
Èç¹uÏe»ñµÃËuÓÐCookieµÄÖµ£¬¿ÉÒÔʹÓò»´«²ÎÊýµÄ½¨£º
public function index(Request $request)
{
$cookies = $request->cookie();
dump($cookies);
}
ÔÙ´ÎÃÎʵØÖ£¬ÎÒÃÇ»aµÃµ½Ò»¸oËuÓÐcookieÖµµÄÊý×e£¬°uÀ¨ÎÒÃǸոÕÉeÖõÄtest£º
array:3 [¨‹
"XSRF-TOKEN" => "CDSXUUYYHJHGDDFGHJAxPNNsVxLHGTRYUGJ"
"laravel_session" => "870a775gthhgef0b9f357edc6r6587878999876556"
"test" => "Hello, Laravel"
]
µ±ÎÒÃÇÐeÒªÔÚ¿Í»§¶ËʹÓõÄʱºo£¬»ñÈ¡CookieµÄÖµ¾Í²»ÊÇÕaÑuÁË¡£Ê×ÏÈ£¬ÎÒÃÇͨ¹ýÏiÓ¦withCookie($cookie)´«Êaµ½¿Í»§¶ËµÄÊý¾Ý²¢²»ÊÇÒ»¸o×Ö*u´®£¬¶øÊÇÒ»¸ocookie¶ÔÏo£º
Cookie {#1490 ¨‹
#name: "test"
#value: "Hello, Laravel"
#domain: null
#expire: 1493791460
#path: "/"
#secure: false
#httpOnly: true
}
»ñÈ¡valueÖµ£¬CookieÀaÌa¹(C)ÁËÒ»¸ogetValue()µÄ½¨À´ÄÃÈ¡¡£ÀýÈçÔÚÄ£°aÖбa¼´uÂe£º
<div>{{ $cookie->getValue() }}</div>
ÔÙ´ÎË¢ÐÂÒ³Ãaeʱ£¬¾Í»aµÃµ½ÉeÖõÄtestµÄcookieÖµ£º
Hello, Laravel
Ça³ýCookie
Ça³ýCookieµÄ½¨±È½Ï¼oµ¥£¬ÔÀiºÍÉeÖÃCookieÒ»ÑuµÄ£¬Ö»Êǽ«¹ýÆÚʱ¼aÉeÖóÉÁ˹ýÈ¥¡£ÕaÀiÒ²ÐeÒª½«Cookie¼ÓÈeµ½HTTPµÄResponseÖУ¬Ê¹ÓÃmake()»oÕßforget()½¨¾u¿É£º
$cookie = Cookie::forget('test');
return Redirect::route('index')->withCookie($cookie);
ÒÔÉÏËuÊoÊÇС±a¸ø´o¼Ò½eÉܵÄLaravel5ÖÐCookieµÄʹÓÃÏe½a£¬Ï£Íu¶Ô´o¼ÒÓÐËu°iÖu£¬Èç¹u´o¼ÒÓÐÈκÎÒÉÎÊÇe¸øÎÒÁoÑÔ£¬Ð¡±a»a¼°Ê±»Ø¸´´o¼ÒµÄ¡£ÔÚ´ËÒ²*dz£¸Ðл´o¼Ò¶Ô½Å±¾Ö®¼ÒÍøÕ¾µÄÖ§³Ö£¡