Files
lgp-admin-plus-api/laravel/redirect.php

106 lines
2.6 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
class Redirect extends Response {
2011-06-08 23:45:08 -05:00
2011-06-14 17:27:11 -05:00
/**
* Create a redirect response.
2011-06-14 17:27:11 -05:00
*
* <code>
* // Create a redirect for the "user/profile" URI
* return Redirect::to('user/profile');
2011-06-14 17:27:11 -05:00
*
* // Create a redirect using the 301 status code
* return Redirect::to('user/profile', 301);
*
* // Create a redirect using the "refresh" method
* return Redirect::to('user/profile', 302, 'refresh');
* </code>
2011-06-08 23:45:08 -05:00
*
* @param string $url
* @param int $status
2011-08-08 23:09:10 -05:00
* @param string $method
2011-06-08 23:45:08 -05:00
* @param bool $https
2011-07-21 23:30:52 -05:00
* @return Redirect
2011-06-08 23:45:08 -05:00
*/
2011-08-08 23:09:10 -05:00
public static function to($url, $status = 302, $method = 'location', $https = false)
2011-06-08 23:45:08 -05:00
{
$url = URL::to($url, $https);
2011-06-08 23:45:08 -05:00
if ($method == 'location')
{
return parent::__construct('', $status)->header('Refresh', '0;url='.$url);
}
else
{
return parent::__construct('', $status)->header('Location', $url);
}
2011-06-14 17:27:11 -05:00
}
/**
* Create a redirect response to a HTTPS URL.
*
* <code>
* // Create a HTTPS redirect to the "user/profile" URI
* return Redirect::to_secure('user/profile');
* </code>
*
* @param string $url
* @param int $status
2011-08-08 23:09:10 -05:00
* @param string $method
* @return Response
*/
2011-08-08 23:09:10 -05:00
public static function to_secure($url, $status = 302, $method = 'location')
{
2011-08-08 23:09:10 -05:00
return static::to($url, $status, $method, true);
}
2011-06-14 17:27:11 -05:00
/**
* Add an item to the session flash data.
*
* This is useful for passing status messages or other temporary data to the next request.
*
* <code>
* // Flash a status message to the session on a redirect
* return Redirect::to('user/profile')->with('status', 'Welcome Back!');
* </code>
*
* @param string $key
* @param mixed $value
2011-06-14 17:27:11 -05:00
* @return Response
*/
public function with($key, $value)
2011-06-14 17:27:11 -05:00
{
IoC::container()->resolve('laravel.session.driver')->flash($key, $value);
2011-07-07 23:55:27 -05:00
2011-06-14 17:27:11 -05:00
return $this;
2011-06-08 23:45:08 -05:00
}
/**
* Magic Method to handle redirecting to named routes.
*
* <code>
* // Create a redirect to the "profile" route
* return Redirect::to_profile();
*
* // Create a redirect to the "profile" route using HTTPS
* return Redirect::to_secure_profile();
* </code>
2011-06-08 23:45:08 -05:00
*/
public static function __callStatic($method, $parameters)
{
$parameters = (isset($parameters[0])) ? $parameters[0] : array();
2011-06-08 23:45:08 -05:00
if (strpos($method, 'to_secure_') === 0)
{
return static::to(URL::to_route(substr($method, 10), $parameters, true));
2011-06-08 23:45:08 -05:00
}
if (strpos($method, 'to_') === 0)
{
return static::to(URL::to_route(substr($method, 3), $parameters));
2011-06-08 23:45:08 -05:00
}
throw new \Exception("Method [$method] is not defined on the Redirect class.");
}
}