Files
spa-api/laravel/redirect.php

98 lines
2.0 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
2011-08-29 22:02:32 -05:00
/**
* The URL generator instance.
*
* @var URL
*/
private $url;
2011-06-14 17:27:11 -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
* @return void
*/
public function __construct(URL $url)
{
$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
*/
public function to($url, $status = 302, $method = 'location', $https = false)
2011-06-08 23:45:08 -05:00
{
$url = $this->url->to($url, $https);
parent::__construct('', $status);
2011-06-08 23:45:08 -05:00
if ($method == 'location')
{
return $this->header('Refresh', '0;url='.$url);
}
else
{
return $this->header('Location', $url);
}
2011-06-14 17:27:11 -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
* @return Response
*/
public function to_secure($url, $status = 302, $method = 'location')
{
return $this->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.
*
* @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')->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-06-08 23:45:08 -05:00
*/
public function __call($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 $this->to($this->url->to_route(substr($method, 10), $parameters, true));
2011-06-08 23:45:08 -05:00
}
if (strpos($method, 'to_') === 0)
{
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.");
}
}