cookie.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php namespace Laravel; use Closure;
  2. class Cookie {
  3. /**
  4. * The cookies that have been set.
  5. *
  6. * @var array
  7. */
  8. public static $jar = array();
  9. /**
  10. * Determine if a cookie exists.
  11. *
  12. * @param string $name
  13. * @return bool
  14. */
  15. public static function has($name)
  16. {
  17. return ! is_null(static::get($name));
  18. }
  19. /**
  20. * Get the value of a cookie.
  21. *
  22. * <code>
  23. * // Get the value of the "favorite" cookie
  24. * $favorite = Cookie::get('favorite');
  25. *
  26. * // Get the value of a cookie or return a default value
  27. * $favorite = Cookie::get('framework', 'Laravel');
  28. * </code>
  29. *
  30. * @param string $name
  31. * @param mixed $default
  32. * @return string
  33. */
  34. public static function get($name, $default = null)
  35. {
  36. if (isset(static::$jar[$name])) return static::$jar[$name];
  37. return array_get(Request::foundation()->cookies->all(), $name, $default);
  38. }
  39. /**
  40. * Set the value of a cookie.
  41. *
  42. * <code>
  43. * // Set the value of the "favorite" cookie
  44. * Cookie::put('favorite', 'Laravel');
  45. *
  46. * // Set the value of the "favorite" cookie for twenty minutes
  47. * Cookie::put('favorite', 'Laravel', 20);
  48. * </code>
  49. *
  50. * @param string $name
  51. * @param string $value
  52. * @param int $expiration
  53. * @param string $path
  54. * @param string $domain
  55. * @param bool $secure
  56. * @return void
  57. */
  58. public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
  59. {
  60. if ($expiration !== 0)
  61. {
  62. $expiration = time() + ($expiration * 60);
  63. }
  64. static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
  65. }
  66. /**
  67. * Set a "permanent" cookie. The cookie will last for one year.
  68. *
  69. * <code>
  70. * // Set a cookie that should last one year
  71. * Cookie::forever('favorite', 'Blue');
  72. * </code>
  73. *
  74. * @param string $name
  75. * @param string $value
  76. * @param string $path
  77. * @param string $domain
  78. * @param bool $secure
  79. * @return bool
  80. */
  81. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  82. {
  83. return static::put($name, $value, 525600, $path, $domain, $secure);
  84. }
  85. /**
  86. * Delete a cookie.
  87. *
  88. * @param string $name
  89. * @param string $path
  90. * @param string $domain
  91. * @param bool $secure
  92. * @return bool
  93. */
  94. public static function forget($name, $path = '/', $domain = null, $secure = false)
  95. {
  96. return static::put($name, null, -2000, $path, $domain, $secure);
  97. }
  98. }