redirect.php 2.7 KB

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