redirect.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php namespace Laravel; use Laravel\Routing\Router;
  2. class Redirect extends Response {
  3. /**
  4. * Create a redirect response to application root.
  5. *
  6. * @param int $status
  7. * @param bool $https
  8. * @return Redirect
  9. */
  10. public static function home($status = 302, $https = null)
  11. {
  12. return static::to(URL::home($https), $status);
  13. }
  14. /**
  15. * Create a redirect response to the HTTP referrer.
  16. *
  17. * @param int $status
  18. * @return Redirect
  19. */
  20. public static function back($status = 302)
  21. {
  22. return static::to(Request::referrer(), $status);
  23. }
  24. /**
  25. * Create a redirect response.
  26. *
  27. * <code>
  28. * // Create a redirect response to a location within the application
  29. * return Redirect::to('user/profile');
  30. *
  31. * // Create a redirect response with a 301 status code
  32. * return Redirect::to('user/profile', 301);
  33. * </code>
  34. *
  35. * @param string $url
  36. * @param int $status
  37. * @param bool $https
  38. * @return Redirect
  39. */
  40. public static function to($url, $status = 302, $https = null)
  41. {
  42. return static::make('', $status)->header('Location', URL::to($url, $https));
  43. }
  44. /**
  45. * Create a redirect response to a HTTPS URL.
  46. *
  47. * @param string $url
  48. * @param int $status
  49. * @return Redirect
  50. */
  51. public static function to_secure($url, $status = 302)
  52. {
  53. return static::to($url, $status, true);
  54. }
  55. /**
  56. * Create a redirect response to a controller action.
  57. *
  58. * @param string $action
  59. * @param array $parameters
  60. * @param int $status
  61. * @return Redirect
  62. */
  63. public static function to_action($action, $parameters = array(), $status = 302)
  64. {
  65. return static::to(URL::to_action($action, $parameters), $status);
  66. }
  67. /**
  68. * Create a redirect response to a named route.
  69. *
  70. * <code>
  71. * // Create a redirect response to the "login" named route
  72. * return Redirect::to_route('login');
  73. *
  74. * // Create a redirect response to the "profile" named route with parameters
  75. * return Redirect::to_route('profile', array($username));
  76. * </code>
  77. *
  78. * @param string $route
  79. * @param array $parameters
  80. * @param int $status
  81. * @return Redirect
  82. */
  83. public static function to_route($route, $parameters = array(), $status = 302)
  84. {
  85. return static::to(URL::to_route($route, $parameters), $status);
  86. }
  87. /**
  88. * Add an item to the session flash data.
  89. *
  90. * This is useful for "passing" status messages or other data to the next request.
  91. *
  92. * <code>
  93. * // Create a redirect response and flash to the session
  94. * return Redirect::to('profile')->with('message', 'Welcome Back!');
  95. * </code>
  96. *
  97. * @param string $key
  98. * @param mixed $value
  99. * @return Redirect
  100. */
  101. public function with($key, $value)
  102. {
  103. if (Config::get('session.driver') == '')
  104. {
  105. throw new \Exception('A session driver must be set before setting flash data.');
  106. }
  107. Session::flash($key, $value);
  108. return $this;
  109. }
  110. /**
  111. * Flash the old input to the session and return the Redirect instance.
  112. *
  113. * Once the input has been flashed, it can be retrieved via the Input::old method.
  114. *
  115. * <code>
  116. * // Redirect and flash all of the input data to the session
  117. * return Redirect::to('login')->with_input();
  118. *
  119. * // Redirect and flash only a few of the input items
  120. * return Redirect::to('login')->with_input('only', array('email', 'username'));
  121. *
  122. * // Redirect and flash all but a few of the input items
  123. * return Redirect::to('login')->with_input('except', array('password', 'ssn'));
  124. * </code>
  125. *
  126. * @param string $filter
  127. * @param array $items
  128. * @return Redirect
  129. */
  130. public function with_input($filter = null, $items = array())
  131. {
  132. Input::flash($filter, $items);
  133. return $this;
  134. }
  135. /**
  136. * Flash a Validator's errors to the session data.
  137. *
  138. * This method allows you to conveniently pass validation errors back to views.
  139. *
  140. * <code>
  141. * // Redirect and flash validator errors the session
  142. * return Redirect::to('register')->with_errors($validator);
  143. * </code>
  144. *
  145. * @param Validator|Messages $container
  146. * @return Redirect
  147. */
  148. public function with_errors($container)
  149. {
  150. $errors = ($container instanceof Validator) ? $container->errors : $container;
  151. return $this->with('errors', $errors);
  152. }
  153. /**
  154. * Send the headers and content of the response to the browser.
  155. *
  156. * @return void
  157. */
  158. public function send()
  159. {
  160. // Dump all output buffering, this ensures
  161. // that symphony will send our redirect headers
  162. // properly if we've outputted any content from
  163. // within Laravel.
  164. while (ob_get_level() > 0)
  165. {
  166. ob_end_clean();
  167. }
  168. return parent::send();
  169. }
  170. }