redirect.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php namespace Laravel;
  2. class Redirect extends Response {
  3. /**
  4. * Create a redirect response.
  5. *
  6. * @param string $url
  7. * @param int $status
  8. * @param bool $https
  9. * @return Redirect
  10. */
  11. public static function to($url, $status = 302, $https = false)
  12. {
  13. $response = new static('', $status);
  14. return $response->header('Location', URL::to($url, $https));
  15. }
  16. /**
  17. * Create a redirect response to a HTTPS URL.
  18. *
  19. * @param string $url
  20. * @param int $status
  21. * @return Response
  22. */
  23. public static function to_secure($url, $status = 302)
  24. {
  25. return static::to($url, $status, true);
  26. }
  27. /**
  28. * Add an item to the session flash data.
  29. *
  30. * This is useful for passing status messages or other temporary data to the next request.
  31. *
  32. * @param string $key
  33. * @param mixed $value
  34. * @return Response
  35. */
  36. public function with($key, $value)
  37. {
  38. if (Config::get('session.driver') == '')
  39. {
  40. throw new \Exception('A session driver must be set before setting flash data.');
  41. }
  42. IoC::container()->resolve('laravel.session')->flash($key, $value);
  43. return $this;
  44. }
  45. /**
  46. * Magic Method to handle creation of redirects to named routes.
  47. */
  48. public static function __callStatic($method, $parameters)
  49. {
  50. $parameters = (isset($parameters[0])) ? $parameters[0] : array();
  51. if (strpos($method, 'to_secure_') === 0)
  52. {
  53. return static::to(URL::to_route(substr($method, 10), $parameters, true));
  54. }
  55. if (strpos($method, 'to_') === 0)
  56. {
  57. return static::to(URL::to_route(substr($method, 3), $parameters));
  58. }
  59. throw new \Exception("Method [$method] is not defined on the Redirect class.");
  60. }
  61. }