先引入下面的2两个很很重要的包包
use Cookie; use Response;
1,特别注意,laravel 的cookie 必须在页面渲染之后才生效,或者说才执行。也就是 return view 之后,模板被渲染才生效;
如:
$cookie_key='todaywork'.date("Ymd",time());
return view('themes.defaults.customer.cluetemporarydetail',[
"customerData" => $customerData,
'source' => IncService::get("customer.source"),
'loginUserData' => $this->permissionService->getLoginedUserInfo(),
'todayWork'=>$request->cookie($cookie_key),//在view 之后用request 获取值
]);
2,怎么设置呢?
$cookie_key='todaywork'.date("Ymd",time()); $todaywork=$request->cookie($cookie_key);
if(empty($todaywork)){
$todayWork=['customer'=>0,'public'=>0,'total'=>$data['total']]; //注意中可是个数组
Cookie::queue($cookie_key, $todayWork, 60*24);//注意设置了有效期24小时
}
3,重点来啦,怎么在ajax 中设置,估计搜半天也找不到答案,哒哒哒哒哒哒,答案就在这里:
$cookie_key='todaywork'.date("Ymd",time());
$todayWork=$request->cookie($cookie_key);
if($data["customer_type"]==1){
$todayWork['customer']=$todayWork['customer']+1;
}
if($data["customer_type"]==2){
$todayWork['public']=$todayWork['public']+1;
}
$cookie=Cookie::make($cookie_key, $todayWork);
return Response::make(json_encode($result))->withCookie($cookie);//灰常有用的一行代码,在make中显示我们必要的输出,在withcookie 中来设置我们的coolie ,这样在ajax 中也就可以用cookie 了,就算ajax 没有也不需要渲染任何的页面,但我们还是可以用cookie ,因为我们用了resposnse 这个类,
game over