redirect.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * Magic Method to handle creation of redirects to named routes.
  62. *
  63. * <code>
  64. * // Create a redirect response to the "profile" named route
  65. * return Redirect::to_profile();
  66. *
  67. * // Create a redirect response to a named route using HTTPS
  68. * return Redirect::to_secure_profile();
  69. * </code>
  70. */
  71. public static function __callStatic($method, $parameters)
  72. {
  73. $status = (isset($parameters[1])) ? $parameters[1] : 302;
  74. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  75. if (strpos($method, 'to_secure_') === 0)
  76. {
  77. return static::to(URL::to_route(substr($method, 10), $parameters, true), $status);
  78. }
  79. if (strpos($method, 'to_') === 0)
  80. {
  81. return static::to(URL::to_route(substr($method, 3), $parameters), $status);
  82. }
  83. throw new \BadMethodCallException("Method [$method] is not defined on the Redirect class.");
  84. }
  85. }