Blogger Information
Blog 81
fans 1
comment 0
visits 124711
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
larvael 设置cookei
有什么是忘不了的的博客
Original
1057 people have browsed it

需要引入的类

use  \Cookie;

use Illuminate\Http\Request;

获取cookie:

public function index(Request $request)
    {
        $request->cookie($key) //方法一
        Cookie::get($key)    //方法二
    }

设置cookie   建议使用方法一

public function index(Request $request)
    {
        Cookie::queue('test', '459', 60*24); //方法一
        $cookie = Cookie::make('test', 'Hello, Laravel', 10)//方法二
        $cookie  =  Cookie::forever('test', 'Hello, Laravel') //方法三 设置永久cookie 实际是有效期5年
        //使用方法二和方法三需要添加使用response()->cookie($cookie)如下面的return
        return response()->view('index.index')->cookie($cookie);
    }

    设置的cookie只能在渲染页面后才能访问,在设置cookie时不能使用内置的dump()函数来打印数据,cookie会设置失败。

删除cookie

public function index(Request $request)
    {
        Cookie::queue(Cookie::forget('test')); //方法一 直接删
        $cookie = Cookie::forget('test')//方法二
        //使用方法二需要添加使用response()->cookie($cookie)如下面的return
        return response()->view('index.index')->cookie($cookie);
    }

使用cookie建议:

    获取cookie:    Cookie::get('test')
    设置cookie :   Cookie::queue('test', '459', 60*24);
    删除cookie:    Cookie::queue(Cookie::forget('test'));


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post