redirect.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php namespace Laravel;
  2. class Redirect extends Response {
  3. /**
  4. * Create a redirect response.
  5. *
  6. * <code>
  7. * // Create a redirect for the "user/profile" URI
  8. * return Redirect::to('user/profile');
  9. *
  10. * // Create a redirect using the 301 status code
  11. * return Redirect::to('user/profile', 301);
  12. *
  13. * // Create a redirect using the "refresh" method
  14. * return Redirect::to('user/profile', 302, 'refresh');
  15. * </code>
  16. *
  17. * @param string $url
  18. * @param int $status
  19. * @param string $method
  20. * @param bool $https
  21. * @return Redirect
  22. */
  23. public static function to($url, $status = 302, $method = 'location', $https = false)
  24. {
  25. $url = URL::to($url, $https);
  26. if ($method == 'location')
  27. {
  28. return parent::__construct('', $status)->header('Refresh', '0;url='.$url);
  29. }
  30. else
  31. {
  32. return parent::__construct('', $status)->header('Location', $url);
  33. }
  34. }
  35. /**
  36. * Create a redirect response to a HTTPS URL.
  37. *
  38. * <code>
  39. * // Create a HTTPS redirect to the "user/profile" URI
  40. * return Redirect::to_secure('user/profile');
  41. * </code>
  42. *
  43. * @param string $url
  44. * @param int $status
  45. * @param string $method
  46. * @return Response
  47. */
  48. public static function to_secure($url, $status = 302, $method = 'location')
  49. {
  50. return static::to($url, $status, $method, true);
  51. }
  52. /**
  53. * Add an item to the session flash data.
  54. *
  55. * This is useful for passing status messages or other temporary data to the next request.
  56. *
  57. * <code>
  58. * // Flash a status message to the session on a redirect
  59. * return Redirect::to('user/profile')->with('status', 'Welcome Back!');
  60. * </code>
  61. *
  62. * @param string $key
  63. * @param mixed $value
  64. * @return Response
  65. */
  66. public function with($key, $value)
  67. {
  68. IoC::container()->resolve('laravel.session.driver')->flash($key, $value);
  69. return $this;
  70. }
  71. /**
  72. * Magic Method to handle redirecting to named routes.
  73. *
  74. * <code>
  75. * // Create a redirect to the "profile" route
  76. * return Redirect::to_profile();
  77. *
  78. * // Create a redirect to the "profile" route using HTTPS
  79. * return Redirect::to_secure_profile();
  80. * </code>
  81. */
  82. public static function __callStatic($method, $parameters)
  83. {
  84. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  85. if (strpos($method, 'to_secure_') === 0)
  86. {
  87. return static::to(URL::to_route(substr($method, 10), $parameters, true));
  88. }
  89. if (strpos($method, 'to_') === 0)
  90. {
  91. return static::to(URL::to_route(substr($method, 3), $parameters));
  92. }
  93. throw new \Exception("Method [$method] is not defined on the Redirect class.");
  94. }
  95. }