Laravel5ÖÐCookieµÄʹÓÃÏê½â

5年以前  |  阅读数:1017 次  |  编程语言:PHP 

½ñÌ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¼Ò¶Ô½Å±¾Ö®¼ÒÍøÕ¾µÄÖ§³Ö£¡

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例