Files
spa-api/laravel/redirect.php

187 lines
4.5 KiB
PHP
Raw Normal View History

2012-02-14 09:38:08 -06:00
<?php namespace Laravel; use Laravel\Routing\Router;
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
2012-02-14 09:38:08 -06:00
/**
* Create a redirect response to application root.
*
* @param int $status
2012-08-03 18:17:48 +03:00
* @param bool $https
2012-02-14 09:38:08 -06:00
* @return Redirect
*/
public static function home($status = 302, $https = null)
2012-02-14 09:38:08 -06:00
{
2012-02-15 08:27:10 -06:00
return static::to(URL::home($https), $status);
2012-02-14 09:38:08 -06:00
}
2012-03-26 15:30:01 -05:00
/**
* Create a redirect response to the HTTP referrer.
*
* @param int $status
* @return Redirect
*/
public static function back($status = 302)
{
return static::to(Request::referrer(), $status);
}
/**
* 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');
*
2012-01-16 13:59:24 -06:00
* // Create a redirect response with a 301 status code
2011-09-22 00:16:14 -05:00
* 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
*/
public static function to($url, $status = 302, $https = null)
2011-06-08 23:45:08 -05:00
{
2011-11-14 21:18:18 -06:00
return static::make('', $status)->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
2012-02-04 21:30:52 +00:00
* @return Redirect
*/
public static function to_secure($url, $status = 302)
{
return static::to($url, $status, true);
}
2012-02-12 14:53:54 -06:00
/**
* Create a redirect response to a controller action.
*
* @param string $action
* @param array $parameters
* @param int $status
* @return Redirect
*/
public static function to_action($action, $parameters = array(), $status = 302)
2012-02-12 14:48:36 -06:00
{
2012-02-12 14:53:54 -06:00
return static::to(URL::to_action($action, $parameters), $status);
2012-02-12 14:48:36 -06:00
}
2012-01-16 13:59:24 -06:00
/**
* Create a redirect response to a named route.
*
* <code>
* // Create a redirect response to the "login" named route
* return Redirect::to_route('login');
*
* // Create a redirect response to the "profile" named route with parameters
* return Redirect::to_route('profile', array($username));
* </code>
*
* @param string $route
* @param array $parameters
* @param int $status
* @return Redirect
*/
2012-02-12 14:48:36 -06:00
public static function to_route($route, $parameters = array(), $status = 302)
2012-01-16 13:59:24 -06:00
{
2012-02-12 14:48:36 -06:00
return static::to(URL::to_route($route, $parameters), $status);
2012-01-16 13:59:24 -06:00
}
2011-06-14 17:27:11 -05:00
/**
* Add an item to the session flash data.
*
2012-01-16 13:59:24 -06:00
* This is useful for "passing" status messages or other data to the next request.
*
2011-09-22 00:16:14 -05:00
* <code>
2012-01-16 13:59:24 -06:00
* // Create a redirect response and flash to the session
* return Redirect::to('profile')->with('message', 'Welcome Back!');
2011-09-22 00:16:14 -05:00
* </code>
*
* @param string $key
* @param mixed $value
2012-02-04 21:30:52 +00:00
* @return Redirect
2011-06-14 17:27:11 -05:00
*/
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
{
2012-01-16 13:59:24 -06:00
throw new \Exception('A session driver must be set before setting flash data.');
2011-09-08 17:49:16 -05:00
}
2012-01-16 13:59:24 -06:00
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-11-22 18:00:17 -06:00
/**
* Flash the old input to the session and return the Redirect instance.
*
* Once the input has been flashed, it can be retrieved via the Input::old method.
*
* <code>
* // Redirect and flash all of the input data to the session
2012-01-16 13:59:24 -06:00
* return Redirect::to('login')->with_input();
2011-11-22 18:00:17 -06:00
*
* // Redirect and flash only a few of the input items
2012-01-16 13:59:24 -06:00
* return Redirect::to('login')->with_input('only', array('email', 'username'));
2011-11-22 18:00:17 -06:00
*
* // Redirect and flash all but a few of the input items
2012-01-16 13:59:24 -06:00
* return Redirect::to('login')->with_input('except', array('password', 'ssn'));
2011-11-22 18:00:17 -06:00
* </code>
*
* @param string $filter
* @param array $items
* @return Redirect
*/
public function with_input($filter = null, $items = array())
{
Input::flash($filter, $items);
2012-01-16 13:59:24 -06:00
2011-11-22 18:00:17 -06:00
return $this;
}
2011-11-23 10:20:25 -06:00
/**
* Flash a Validator's errors to the session data.
*
* This method allows you to conveniently pass validation errors back to views.
*
* <code>
2012-01-16 13:59:24 -06:00
* // Redirect and flash validator errors the session
2011-11-23 10:20:25 -06:00
* return Redirect::to('register')->with_errors($validator);
* </code>
*
* @param Validator|Messages $container
2011-11-23 10:20:25 -06:00
* @return Redirect
*/
public function with_errors($container)
2011-11-23 10:20:25 -06:00
{
$errors = ($container instanceof Validator) ? $container->errors : $container;
return $this->with('errors', $errors);
2011-11-23 10:20:25 -06:00
}
/**
* Send the headers and content of the response to the browser.
*
* @return void
*/
public function send()
{
// Dump all output buffering, this ensures
// that symphony will send our redirect headers
// properly if we've outputted any content from
// within Laravel.
while (ob_get_level() > 0)
{
ob_end_clean();
}
return parent::send();
}
2011-11-23 10:20:25 -06:00
}