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
|
|
|
|
2011-08-29 22:02:32 -05:00
|
|
|
/**
|
|
|
|
|
* The URL generator instance.
|
|
|
|
|
*
|
|
|
|
|
* @var URL
|
|
|
|
|
*/
|
|
|
|
|
private $url;
|
|
|
|
|
|
2011-06-14 17:27:11 -05:00
|
|
|
/**
|
2011-08-29 00:27:03 -05:00
|
|
|
* Create a new redirect generator instance.
|
2011-06-14 17:27:11 -05:00
|
|
|
*
|
2011-08-29 22:02:32 -05:00
|
|
|
* @param Session\Driver $session
|
|
|
|
|
* @param URL $url
|
2011-08-29 00:27:03 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-08-30 22:09:47 -05:00
|
|
|
public function __construct(URL $url)
|
2011-08-29 00:27:03 -05:00
|
|
|
{
|
|
|
|
|
$this->url = $url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a redirect response.
|
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-29 00:27:03 -05:00
|
|
|
public function to($url, $status = 302, $method = 'location', $https = false)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-29 00:27:03 -05:00
|
|
|
$url = $this->url->to($url, $https);
|
|
|
|
|
|
|
|
|
|
parent::__construct('', $status);
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-08-24 22:51:32 -05:00
|
|
|
if ($method == 'location')
|
|
|
|
|
{
|
2011-08-29 00:27:03 -05:00
|
|
|
return $this->header('Refresh', '0;url='.$url);
|
2011-08-24 22:51:32 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-08-29 00:27:03 -05:00
|
|
|
return $this->header('Location', $url);
|
2011-08-24 22:51:32 -05:00
|
|
|
}
|
2011-06-14 17:27:11 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-17 00:02:24 -05:00
|
|
|
/**
|
|
|
|
|
* Create a redirect response to a HTTPS URL.
|
|
|
|
|
*
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @param int $status
|
2011-08-08 23:09:10 -05:00
|
|
|
* @param string $method
|
2011-06-17 00:02:24 -05:00
|
|
|
* @return Response
|
|
|
|
|
*/
|
2011-08-29 00:27:03 -05:00
|
|
|
public function to_secure($url, $status = 302, $method = 'location')
|
2011-06-17 00:02:24 -05:00
|
|
|
{
|
2011-08-29 00:27:03 -05:00
|
|
|
return $this->to($url, $status, $method, true);
|
2011-06-17 00:02:24 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-14 17:27:11 -05:00
|
|
|
/**
|
|
|
|
|
* Add an item to the session flash data.
|
|
|
|
|
*
|
2011-08-24 22:51:32 -05:00
|
|
|
* This is useful for passing status messages or other temporary data to the next request.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $value
|
2011-06-14 17:27:11 -05:00
|
|
|
* @return Response
|
|
|
|
|
*/
|
2011-08-25 22:53:05 -05:00
|
|
|
public function with($key, $value)
|
2011-06-14 17:27:11 -05:00
|
|
|
{
|
2011-08-30 22:09:47 -05:00
|
|
|
IoC::container()->resolve('laravel.session')->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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-08-29 00:27:03 -05:00
|
|
|
* Magic Method to handle creation of redirects to named routes.
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2011-08-29 00:27:03 -05:00
|
|
|
public function __call($method, $parameters)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-06-30 07:45:38 -07:00
|
|
|
$parameters = (isset($parameters[0])) ? $parameters[0] : array();
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
if (strpos($method, 'to_secure_') === 0)
|
|
|
|
|
{
|
2011-08-29 00:27:03 -05:00
|
|
|
return $this->to($this->url->to_route(substr($method, 10), $parameters, true));
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strpos($method, 'to_') === 0)
|
|
|
|
|
{
|
2011-08-29 00:27:03 -05:00
|
|
|
return $this->to($this->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.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|