Redirect.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\response;
  12. use think\Response;
  13. class Redirect extends Response
  14. {
  15. protected $options = [];
  16. // URL参数
  17. protected $params = [];
  18. public function __construct($data = '', $code = 302, array $header = [], array $options = [])
  19. {
  20. parent::__construct($data, $code, $header, $options);
  21. $this->cacheControl('no-cache,must-revalidate');
  22. }
  23. /**
  24. * 处理数据
  25. * @access protected
  26. * @param mixed $data 要处理的数据
  27. * @return mixed
  28. */
  29. protected function output($data)
  30. {
  31. $this->header['Location'] = $this->getTargetUrl();
  32. return;
  33. }
  34. /**
  35. * 重定向传值(通过Session)
  36. * @access protected
  37. * @param string|array $name 变量名或者数组
  38. * @param mixed $value 值
  39. * @return $this
  40. */
  41. public function with($name, $value = null)
  42. {
  43. $session = $this->app['session'];
  44. if (is_array($name)) {
  45. foreach ($name as $key => $val) {
  46. $session->flash($key, $val);
  47. }
  48. } else {
  49. $session->flash($name, $value);
  50. }
  51. return $this;
  52. }
  53. /**
  54. * 获取跳转地址
  55. * @access public
  56. * @return string
  57. */
  58. public function getTargetUrl()
  59. {
  60. if (strpos($this->data, '://') || (0 === strpos($this->data, '/') && empty($this->params))) {
  61. return $this->data;
  62. } else {
  63. return $this->app['url']->build($this->data, $this->params);
  64. }
  65. }
  66. public function params($params = [])
  67. {
  68. $this->params = $params;
  69. return $this;
  70. }
  71. /**
  72. * 记住当前url后跳转
  73. * @access public
  74. * @param string $url 指定记住的url
  75. * @return $this
  76. */
  77. public function remember($url = null)
  78. {
  79. $this->app['session']->set('redirect_url', $url ?: $this->app['request']->url());
  80. return $this;
  81. }
  82. /**
  83. * 跳转到上次记住的url
  84. * @access public
  85. * @param string $url 闪存数据不存在时的跳转地址
  86. * @return $this
  87. */
  88. public function restore($url = null)
  89. {
  90. $session = $this->app['session'];
  91. if ($session->has('redirect_url')) {
  92. $this->data = $session->get('redirect_url');
  93. $session->delete('redirect_url');
  94. } elseif ($url) {
  95. $this->data = $url;
  96. }
  97. return $this;
  98. }
  99. }