Files
spa-api/laravel/redirect.php

97 lines
2.4 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
2011-08-30 22:35:32 -05:00
class Redirect extends Response {
2011-06-08 23:45:08 -05:00
/**
* Create a redirect response.
2011-06-08 23:45:08 -05:00
*
2011-09-22 00:16:14 -05:00
* <code>
* // Create a redirect response to a location within the application
* return Redirect::to('user/profile');
*
* // Create a redirect with a 301 status code
* return Redirect::to('user/profile', 301);
*
* // Create a redirect response to a location outside of the application
* return Redirect::to('http://google.com');
* </code>
*
2011-06-08 23:45:08 -05:00
* @param string $url
* @param int $status
* @param bool $https
2011-07-21 23:30:52 -05:00
* @return Redirect
2011-06-08 23:45:08 -05:00
*/
public static function to($url, $status = 302, $https = false)
2011-06-08 23:45:08 -05:00
{
$response = new static('', $status);
2011-06-08 23:45:08 -05:00
return $response->header('Location', URL::to($url, $https));
2011-06-14 17:27:11 -05:00
}
/**
* Create a redirect response to a HTTPS URL.
*
* @param string $url
* @param int $status
* @return Response
*/
public static function to_secure($url, $status = 302)
{
return static::to($url, $status, 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.
*
2011-09-22 00:16:14 -05:00
* <code>
* // Create a redirect response and flash something to the session
* return Redirect::to('user/profile')->with('message', '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
{
if (Config::get('session.driver') == '')
2011-09-08 17:49:16 -05:00
{
throw new \Exception('A session driver must be set before setting flash data.');
}
2011-10-10 21:34:15 -05:00
Session\Manager::$payload->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 creation of redirects to named routes.
2011-09-22 00:16:14 -05:00
*
* <code>
* // Create a redirect response to the "profile" named route
* return Redirect::to_profile();
*
* // Create a redirect response to a named route using HTTPS
* return Redirect::to_secure_profile();
* </code>
2011-06-08 23:45:08 -05:00
*/
public static function __callStatic($method, $parameters)
2011-06-08 23:45:08 -05:00
{
$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.");
}
}