Session

以下是一个封装好的Session操作类,可以简化对Session的操作,同时也展示了如何用框架本身的方法操作Session

  1. <?php
  2. class SessionFacade
  3. {
  4. /**
  5. * Set Session
  6. * @param $name
  7. * @param $value
  8. * @author : evalor <master@evalor.cn>
  9. * @return bool
  10. */
  11. static function set($name, $value = null)
  12. {
  13. $SessionInstance = Response::getInstance()->session();
  14. if (is_array($name)) {
  15. try {
  16. foreach ($name as $sessionName => $sessionValue) {
  17. $SessionInstance->set($sessionName, $sessionValue);
  18. }
  19. return true;
  20. } catch (\Exception $exception) {
  21. return false;
  22. }
  23. } else {
  24. return $SessionInstance->set($name, $value);
  25. }
  26. }
  27. /**
  28. * Get Session
  29. * @param $name
  30. * @param $default
  31. * @author : evalor <master@evalor.cn>
  32. * @return mixed|null
  33. */
  34. static function find($name, $default = null)
  35. {
  36. $SessionInstance = Request::getInstance()->session();
  37. return $SessionInstance->get($name, $default);
  38. }
  39. /**
  40. * Check Session exists
  41. * @param $name
  42. * @author : evalor <master@evalor.cn>
  43. * @return bool
  44. */
  45. static function has($name)
  46. {
  47. return static::find($name, null) !== null;
  48. }
  49. /**
  50. * Delete Session Values
  51. * @param $name
  52. * @author : evalor <master@evalor.cn>
  53. * @return bool|int
  54. */
  55. static function delete($name)
  56. {
  57. $SessionInstance = Response::getInstance()->session();
  58. return $SessionInstance->set($name, null);
  59. }
  60. /**
  61. * Clear Session
  62. * @author : evalor <master@evalor.cn>
  63. */
  64. static function clear()
  65. {
  66. $Response = Response::getInstance();
  67. $SessionInstance = $Response->session();
  68. $SessionInstance->destroy();
  69. $Response->setCookie($SessionInstance->sessionName(), null, 0);
  70. }
  71. }