laravel5.1設置cookie
阿新 • • 發佈:2017-05-07
name eve space con for one with upd namespace
Laravel 所建立的 cookie 會加密並且加上認證記號,這代表著被用戶擅自更改的 cookie 會失效。從請求中取得Cookie值,你使用cookie方法
$value = $request->cookie(‘name‘);
還可以使用輔助方法$value = Request::cookie(‘name‘);
加上新的 Cookie 到響應
輔助方法 cookie
提供一個簡易的工廠方法來產生新的 Symfony\Component\HttpFoundation\Cookie
實例。可以在 Response
實例之後連接withCookie
方法帶入 cookie 至響應:
$response = new Illuminate\Http\Response(‘Hello World‘);
$response->withCookie(cookie(‘name‘, ‘value‘, $minutes));
return $response;
建立永久有效的 Cookie*
雖然說是「永遠」,但真正的意思是五年。
$response->withCookie(cookie()->forever(‘name‘, ‘value‘));
Queueing Cookies
You may also "queue" a cookie to be added to the outgoing response, even before that response has been created:
<?php namespace App\Http\Controllers;
use Cookie;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Update a resource
*
* @return Response
*/
public function update()
{
Cookie::queue(‘name‘, ‘value‘);
return response(‘Hello World‘);
}
}
laravel5.1設置cookie