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

120 lines
2.7 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-09-08 17:49:16 -05:00
* @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
*
2011-09-08 17:49:16 -05:00
* <code>
* // Create a redirect response to a given URL
* return Redirect::to('user/profile');
*
* // Create a redirect with a given status code
* return Redirect::to('user/profile', 301);
* </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
*/
2011-09-08 17:49:16 -05:00
public function to($url, $status = 302, $https = false)
2011-06-08 23:45:08 -05:00
{
parent::__construct('', $status);
2011-06-08 23:45:08 -05:00
2011-09-08 17:49:16 -05:00
return $this->header('Location', $this->url->to($url, $https));
2011-06-14 17:27:11 -05:00
}
/**
* Create a redirect response to a HTTPS URL.
*
2011-09-08 17:49:16 -05:00
* <code>
* // Create a redirect response to a HTTPS URL
* return Redirect::to_secure('user/profile');
* </code>
*
* @param string $url
* @param int $status
* @return Response
*/
2011-09-08 17:49:16 -05:00
public function to_secure($url, $status = 302)
{
2011-09-08 17:49:16 -05:00
return $this->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.
*
* <code>
* // Create a redirect and flash a messages to the session
* return Redirect::to_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
{
2011-09-08 17:49:16 -05:00
if (IoC::container()->resolve('laravel.config')->get('session.driver') == '')
{
throw new \Exception('A session driver must be set before setting flash data.');
}
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.
*
* <code>
* // Create a redirect to the "profile" route
* return Redirect::to_profile();
*
* // Create a redirect to the "profile" route with wildcard segments
* return Redirect::to_profile(array($username));
*
* // Create a redirect to the "profile" route using HTTPS
* return Redirect::to_secure_profile();
* </code>
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.");
}
}