CCookie.php 935 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fec\helpers;
  10. use Yii;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class CCookie
  16. {
  17. public static function getCookieM(){
  18. return Yii::$app->request->cookies;
  19. }
  20. # 1.是否存在某个cookie
  21. public static function hasCookie($key){
  22. return self::getCookieM()->has($key);
  23. }
  24. # 2.得到某个cookie的值
  25. public static function getCookie($key,$default=''){
  26. return self::getCookieM()->getValue($key,$default);
  27. }
  28. # 3.设置cookie
  29. public static function setCookie($key,$val){
  30. self::getCookieM()->add(new \yii\web\Cookie([
  31. 'name' => $key,
  32. 'value' => $val,
  33. ]));
  34. }
  35. # 4.删除cookie
  36. public static function removeCookie($key){
  37. return self::getCookieM()->remove($key);
  38. }
  39. }