redirect.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php namespace Laravel;
  2. class Redirect_Facade extends Facade {
  3. public static $resolve = 'redirect';
  4. }
  5. class Redirect extends Response {
  6. /**
  7. * The URL generator instance.
  8. *
  9. * @var URL
  10. */
  11. private $url;
  12. /**
  13. * Create a new redirect generator instance.
  14. *
  15. * @param Session\Driver $session
  16. * @param URL $url
  17. * @return void
  18. */
  19. public function __construct(URL $url)
  20. {
  21. $this->url = $url;
  22. }
  23. /**
  24. * Create a redirect response.
  25. *
  26. * @param string $url
  27. * @param int $status
  28. * @param string $method
  29. * @param bool $https
  30. * @return Redirect
  31. */
  32. public function to($url, $status = 302, $method = 'location', $https = false)
  33. {
  34. $url = $this->url->to($url, $https);
  35. parent::__construct('', $status);
  36. if ($method == 'location')
  37. {
  38. return $this->header('Refresh', '0;url='.$url);
  39. }
  40. else
  41. {
  42. return $this->header('Location', $url);
  43. }
  44. }
  45. /**
  46. * Create a redirect response to a HTTPS URL.
  47. *
  48. * @param string $url
  49. * @param int $status
  50. * @param string $method
  51. * @return Response
  52. */
  53. public function to_secure($url, $status = 302, $method = 'location')
  54. {
  55. return $this->to($url, $status, $method, true);
  56. }
  57. /**
  58. * Add an item to the session flash data.
  59. *
  60. * This is useful for passing status messages or other temporary data to the next request.
  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')->flash($key, $value);
  69. return $this;
  70. }
  71. /**
  72. * Magic Method to handle creation of redirects to named routes.
  73. */
  74. public function __call($method, $parameters)
  75. {
  76. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  77. if (strpos($method, 'to_secure_') === 0)
  78. {
  79. return $this->to($this->url->to_route(substr($method, 10), $parameters, true));
  80. }
  81. if (strpos($method, 'to_') === 0)
  82. {
  83. return $this->to($this->url->to_route(substr($method, 3), $parameters));
  84. }
  85. throw new \Exception("Method [$method] is not defined on the Redirect class.");
  86. }
  87. }