首页 > 代码库 > laravel5.1设置cookie

laravel5.1设置cookie

Laravel 所建立的 cookie 会加密并且加上认证记号,这代表着被用户擅自更改的 cookie 会失效。从请求中取得Cookie值,你使用cookie方法

$value = http://www.mamicode.com/$request->cookie(‘name‘);
还可以使用辅助方法
$value = http://www.mamicode.com/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