redirect.php 3.96 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel; use Laravel\Routing\Router;
2

Taylor Otwell committed
3
class Redirect extends Response {
4 5

	/**
Taylor Otwell committed
6 7 8 9 10 11 12 13
	 * Create a redirect response to application root.
	 *
	 * @param  int       $status
	 * @param  bool      $secure
	 * @return Redirect
	 */
	public static function home($status = 302, $https = false)
	{
Taylor Otwell committed
14
		return static::to(URL::home($https), $status);
Taylor Otwell committed
15 16 17
	}

	/**
18
	 * Create a redirect response.
19
	 *
20 21 22 23
	 * <code>
	 *		// Create a redirect response to a location within the application
	 *		return Redirect::to('user/profile');
	 *
24
	 *		// Create a redirect response with a 301 status code
25 26 27
	 *		return Redirect::to('user/profile', 301);
	 * </code>
	 *
28 29 30
	 * @param  string    $url
	 * @param  int       $status
	 * @param  bool      $https
31
	 * @return Redirect
32
	 */
33
	public static function to($url, $status = 302, $https = false)
34
	{
35
		return static::make('', $status)->header('Location', URL::to($url, $https));
36 37 38
	}

	/**
39 40 41 42
	 * Create a redirect response to a HTTPS URL.
	 *
	 * @param  string    $url
	 * @param  int       $status
Phill Sparks committed
43
	 * @return Redirect
44
	 */
45
	public static function to_secure($url, $status = 302)
46
	{
47
		return static::to($url, $status, true);
48 49
	}

50 51 52 53 54 55 56 57 58
	/**
	 * 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)
59
	{
60
		return static::to(URL::to_action($action, $parameters), $status);
61 62
	}

63
	/**
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	 * 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
	 */
79
	public static function to_route($route, $parameters = array(), $status = 302)
80
	{
81
		return static::to(URL::to_route($route, $parameters), $status);
82 83 84
	}

	/**
85 86
	 * Add an item to the session flash data.
	 *
87
	 * This is useful for "passing" status messages or other data to the next request.
88
	 *
89
	 * <code>
90 91
	 *		// Create a redirect response and flash to the session
	 *		return Redirect::to('profile')->with('message', 'Welcome Back!');
92 93
	 * </code>
	 *
94 95
	 * @param  string          $key
	 * @param  mixed           $value
Phill Sparks committed
96
	 * @return Redirect
97
	 */
98
	public function with($key, $value)
99
	{
100
		if (Config::get('session.driver') == '')
101
		{
102
			throw new \Exception('A session driver must be set before setting flash data.');
103 104
		}

105
		Session::flash($key, $value);
106

107
		return $this;
108 109 110
	}

	/**
111 112 113 114 115 116
	 * 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
117
	 *		return Redirect::to('login')->with_input();
118 119
	 *
	 *		// Redirect and flash only a few of the input items
120
	 *		return Redirect::to('login')->with_input('only', array('email', 'username'));
121 122
	 *
	 *		// Redirect and flash all but a few of the input items
123
	 *		return Redirect::to('login')->with_input('except', array('password', 'ssn'));
124 125 126 127 128 129 130 131 132
	 * </code>
	 *
	 * @param  string    $filter
	 * @param  array     $items
	 * @return Redirect
	 */
	public function with_input($filter = null, $items = array())
	{
		Input::flash($filter, $items);
133

134 135 136 137
		return $this;
	}

	/**
138 139 140 141 142
	 * Flash a Validator's errors to the session data.
	 *
	 * This method allows you to conveniently pass validation errors back to views.
	 *
	 * <code>
143
	 *		// Redirect and flash validator errors the session
144 145 146
	 *		return Redirect::to('register')->with_errors($validator);
	 * </code>
	 *
147
	 * @param  Validator|Messages  $container
148 149
	 * @return Redirect
	 */
150
	public function with_errors($container)
151
	{
152 153 154
		$errors = ($container instanceof Validator) ? $container->errors : $container;

		return $this->with('errors', $errors);
155 156 157
	}

}