redirect.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 with a 301 status code
  11. * return Redirect::to('user/profile', 301);
  12. *
  13. * // Create a redirect response to a location outside of the application
  14. * return Redirect::to('http://google.com');
  15. * </code>
  16. *
  17. * @param string $url
  18. * @param int $status
  19. * @param bool $https
  20. * @return Redirect
  21. */
  22. public static function to($url, $status = 302, $https = false)
  23. {
  24. return static::make('', $status)->header('Location', URL::to($url, $https));
  25. }
  26. /**
  27. * Create a redirect response to a HTTPS URL.
  28. *
  29. * @param string $url
  30. * @param int $status
  31. * @return Response
  32. */
  33. public static function to_secure($url, $status = 302)
  34. {
  35. return static::to($url, $status, true);
  36. }
  37. /**
  38. * Add an item to the session flash data.
  39. *
  40. * This is useful for passing status messages or other temporary data to the next request.
  41. *
  42. * <code>
  43. * // Create a redirect response and flash something to the session
  44. * return Redirect::to('user/profile')->with('message', 'Welcome Back!');
  45. * </code>
  46. *
  47. * @param string $key
  48. * @param mixed $value
  49. * @return Response
  50. */
  51. public function with($key, $value)
  52. {
  53. if (Config::get('session.driver') == '')
  54. {
  55. throw new \LogicException('A session driver must be set before setting flash data.');
  56. }
  57. IoC::core('session')->flash($key, $value);
  58. return $this;
  59. }
  60. /**
  61. * Flash the old input to the session and return the Redirect instance.
  62. *
  63. * Once the input has been flashed, it can be retrieved via the Input::old method.
  64. *
  65. * <code>
  66. * // Redirect and flash all of the input data to the session
  67. * return Redirect::to_login()->with_input();
  68. *
  69. * // Redirect and flash only a few of the input items
  70. * return Redirect::to_login()->with_input('only', array('email', 'username'));
  71. *
  72. * // Redirect and flash all but a few of the input items
  73. * return Redirect::to_login()->with_input('except', array('password', 'ssn'));
  74. * </code>
  75. *
  76. * @param string $filter
  77. * @param array $items
  78. * @return Redirect
  79. */
  80. public function with_input($filter = null, $items = array())
  81. {
  82. Input::flash($filter, $items);
  83. return $this;
  84. }
  85. /**
  86. * Flash a Validator's errors to the session data.
  87. *
  88. * This method allows you to conveniently pass validation errors back to views.
  89. *
  90. * <code>
  91. * // Redirect and flash a validator's errors the session
  92. * return Redirect::to('register')->with_errors($validator);
  93. *
  94. * // Redirect and flash a message container to the session
  95. * return Redirect::to('register')->with_errors($messages);
  96. * </code>
  97. *
  98. * @param Validator|Messages $container
  99. * @return Redirect
  100. */
  101. public function with_errors($container)
  102. {
  103. $errors = ($container instanceof Validator) ? $container->errors : $container;
  104. return $this->with('errors', $errors);
  105. }
  106. /**
  107. * Magic Method to handle creation of redirects to named routes.
  108. *
  109. * <code>
  110. * // Create a redirect response to the "profile" named route
  111. * return Redirect::to_profile();
  112. *
  113. * // Create a redirect response to a named route using HTTPS
  114. * return Redirect::to_secure_profile();
  115. *
  116. * // Create a redirect response to a named route with wildcard parameters
  117. * return Redirect::to_profile(array($username));
  118. * </code>
  119. */
  120. public static function __callStatic($method, $parameters)
  121. {
  122. // Extract the parameters that should be placed in the URL. These parameters
  123. // are used to fill all of the wildcard slots in the route URI definition.
  124. // They are passed as the first parameter to this magic method.
  125. $wildcards = (isset($parameters[0])) ? $parameters[0] : array();
  126. $status = (isset($parameters[1])) ? $parameters[1] : 302;
  127. if (strpos($method, 'to_secure_') === 0)
  128. {
  129. return static::to(URL::to_route(substr($method, 10), $wildcards, true), $status);
  130. }
  131. if (strpos($method, 'to_') === 0)
  132. {
  133. return static::to(URL::to_route(substr($method, 3), $wildcards), $status);
  134. }
  135. throw new \BadMethodCallException("Method [$method] is not defined on the Redirect class.");
  136. }
  137. }