redirect.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php namespace System;
  2. class Redirect {
  3. /**
  4. * Create a redirect response.
  5. *
  6. * @param string $url
  7. * @param string $method
  8. * @param int $status
  9. * @param bool $https
  10. * @return Response
  11. */
  12. public static function to($url, $method = 'location', $status = 302, $https = false)
  13. {
  14. $url = URL::to($url, $https);
  15. return ($method == 'refresh')
  16. ? Response::make('', $status)->header('Refresh', '0;url='.$url)
  17. : Response::make('', $status)->header('Location', $url);
  18. }
  19. /**
  20. * Create a redirect response to a HTTPS URL.
  21. *
  22. * @param string $url
  23. * @param string $method
  24. * @param int $status
  25. * @return Response
  26. */
  27. public static function to_secure($url, $method = 'location', $status = 302)
  28. {
  29. return static::to($url, $method, $status, true);
  30. }
  31. /**
  32. * Magic Method to handle redirecting to routes.
  33. */
  34. public static function __callStatic($method, $parameters)
  35. {
  36. // ----------------------------------------------------
  37. // Dynamically redirect to a secure route URL.
  38. // ----------------------------------------------------
  39. if (strpos($method, 'to_secure_') === 0)
  40. {
  41. return static::to(URL::to_route(substr($method, 10), $parameters, true));
  42. }
  43. // ----------------------------------------------------
  44. // Dynamically redirect a route URL.
  45. // ----------------------------------------------------
  46. if (strpos($method, 'to_') === 0)
  47. {
  48. return static::to(URL::to_route(substr($method, 3), $parameters));
  49. }
  50. throw new \Exception("Method [$method] is not defined on the Redirect class.");
  51. }
  52. }