redirect.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php namespace Laravel;
  2. class Redirect extends Response {
  3. /**
  4. * Create a redirect response.
  5. *
  6. * <code>
  7. * // Create a redirect response to a location within the application
  8. * return Redirect::to('user/profile');
  9. *
  10. * // Create a redirect response with a 301 status code
  11. * return Redirect::to('user/profile', 301);
  12. * </code>
  13. *
  14. * @param string $url
  15. * @param int $status
  16. * @param bool $https
  17. * @return Redirect
  18. */
  19. public static function to($url, $status = 302, $https = false)
  20. {
  21. return static::make('', $status)->header('Location', URL::to($url, $https));
  22. }
  23. /**
  24. * Create a redirect response to a HTTPS URL.
  25. *
  26. * @param string $url
  27. * @param int $status
  28. * @return Redirect
  29. */
  30. public static function to_secure($url, $status = 302)
  31. {
  32. return static::to($url, $status, true);
  33. }
  34. /**
  35. * Create a redirect response to a controller action.
  36. *
  37. * @param string $action
  38. * @param array $parameters
  39. * @param int $status
  40. * @return Redirect
  41. */
  42. public static function to_action($action, $parameters = array(), $status = 302)
  43. {
  44. return static::to(URL::to_action($action, $parameters), $status);
  45. }
  46. /**
  47. * Create a redirect response to a named route.
  48. *
  49. * <code>
  50. * // Create a redirect response to the "login" named route
  51. * return Redirect::to_route('login');
  52. *
  53. * // Create a redirect response to the "profile" named route with parameters
  54. * return Redirect::to_route('profile', array($username));
  55. * </code>
  56. *
  57. * @param string $route
  58. * @param array $parameters
  59. * @param int $status
  60. * @return Redirect
  61. */
  62. public static function to_route($route, $parameters = array(), $status = 302)
  63. {
  64. return static::to(URL::to_route($route, $parameters), $status);
  65. }
  66. /**
  67. * Add an item to the session flash data.
  68. *
  69. * This is useful for "passing" status messages or other data to the next request.
  70. *
  71. * <code>
  72. * // Create a redirect response and flash to the session
  73. * return Redirect::to('profile')->with('message', 'Welcome Back!');
  74. * </code>
  75. *
  76. * @param string $key
  77. * @param mixed $value
  78. * @return Redirect
  79. */
  80. public function with($key, $value)
  81. {
  82. if (Config::get('session.driver') == '')
  83. {
  84. throw new \Exception('A session driver must be set before setting flash data.');
  85. }
  86. Session::flash($key, $value);
  87. return $this;
  88. }
  89. /**
  90. * Flash the old input to the session and return the Redirect instance.
  91. *
  92. * Once the input has been flashed, it can be retrieved via the Input::old method.
  93. *
  94. * <code>
  95. * // Redirect and flash all of the input data to the session
  96. * return Redirect::to('login')->with_input();
  97. *
  98. * // Redirect and flash only a few of the input items
  99. * return Redirect::to('login')->with_input('only', array('email', 'username'));
  100. *
  101. * // Redirect and flash all but a few of the input items
  102. * return Redirect::to('login')->with_input('except', array('password', 'ssn'));
  103. * </code>
  104. *
  105. * @param string $filter
  106. * @param array $items
  107. * @return Redirect
  108. */
  109. public function with_input($filter = null, $items = array())
  110. {
  111. Input::flash($filter, $items);
  112. return $this;
  113. }
  114. /**
  115. * Flash a Validator's errors to the session data.
  116. *
  117. * This method allows you to conveniently pass validation errors back to views.
  118. *
  119. * <code>
  120. * // Redirect and flash validator errors the session
  121. * return Redirect::to('register')->with_errors($validator);
  122. * </code>
  123. *
  124. * @param Validator|Messages $container
  125. * @return Redirect
  126. */
  127. public function with_errors($container)
  128. {
  129. $errors = ($container instanceof Validator) ? $container->errors : $container;
  130. return $this->with('errors', $errors);
  131. }
  132. }