redirect.php 4.53 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
	 * Create a redirect response to application root.
	 *
	 * @param  int       $status
Sergii Grebeniuk committed
9
	 * @param  bool      $https
Taylor Otwell committed
10 11
	 * @return Redirect
	 */
12
	public static function home($status = 302, $https = null)
Taylor Otwell committed
13
	{
Taylor Otwell committed
14
		return static::to(URL::home($https), $status);
Taylor Otwell committed
15 16 17
	}

	/**
18 19 20 21 22 23 24 25 26 27 28
	 * 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);
	}

	/**
29
	 * Create a redirect response.
30
	 *
31 32 33 34
	 * <code>
	 *		// Create a redirect response to a location within the application
	 *		return Redirect::to('user/profile');
	 *
35
	 *		// Create a redirect response with a 301 status code
36 37 38
	 *		return Redirect::to('user/profile', 301);
	 * </code>
	 *
39 40 41
	 * @param  string    $url
	 * @param  int       $status
	 * @param  bool      $https
42
	 * @return Redirect
43
	 */
44
	public static function to($url, $status = 302, $https = null)
45
	{
46
		return static::make('', $status)->header('Location', URL::to($url, $https));
47 48 49
	}

	/**
50 51 52 53
	 * Create a redirect response to a HTTPS URL.
	 *
	 * @param  string    $url
	 * @param  int       $status
Phill Sparks committed
54
	 * @return Redirect
55
	 */
56
	public static function to_secure($url, $status = 302)
57
	{
58
		return static::to($url, $status, true);
59 60
	}

61 62 63 64 65 66 67 68 69
	/**
	 * 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)
70
	{
71
		return static::to(URL::to_action($action, $parameters), $status);
72 73
	}

74
	/**
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	 * 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
	 */
90
	public static function to_route($route, $parameters = array(), $status = 302)
91
	{
92
		return static::to(URL::to_route($route, $parameters), $status);
93 94 95
	}

	/**
96 97
	 * Add an item to the session flash data.
	 *
98
	 * This is useful for "passing" status messages or other data to the next request.
99
	 *
100
	 * <code>
101 102
	 *		// Create a redirect response and flash to the session
	 *		return Redirect::to('profile')->with('message', 'Welcome Back!');
103 104
	 * </code>
	 *
105 106
	 * @param  string          $key
	 * @param  mixed           $value
Phill Sparks committed
107
	 * @return Redirect
108
	 */
109
	public function with($key, $value)
110
	{
111
		if (Config::get('session.driver') == '')
112
		{
113
			throw new \Exception('A session driver must be set before setting flash data.');
114 115
		}

116
		Session::flash($key, $value);
117

118
		return $this;
119 120 121
	}

	/**
122 123 124 125 126 127
	 * 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
128
	 *		return Redirect::to('login')->with_input();
129 130
	 *
	 *		// Redirect and flash only a few of the input items
131
	 *		return Redirect::to('login')->with_input('only', array('email', 'username'));
132 133
	 *
	 *		// Redirect and flash all but a few of the input items
134
	 *		return Redirect::to('login')->with_input('except', array('password', 'ssn'));
135 136 137 138 139 140 141 142 143
	 * </code>
	 *
	 * @param  string    $filter
	 * @param  array     $items
	 * @return Redirect
	 */
	public function with_input($filter = null, $items = array())
	{
		Input::flash($filter, $items);
144

145 146 147 148
		return $this;
	}

	/**
149 150 151 152 153
	 * Flash a Validator's errors to the session data.
	 *
	 * This method allows you to conveniently pass validation errors back to views.
	 *
	 * <code>
154
	 *		// Redirect and flash validator errors the session
155 156 157
	 *		return Redirect::to('register')->with_errors($validator);
	 * </code>
	 *
158
	 * @param  Validator|Messages  $container
159 160
	 * @return Redirect
	 */
161
	public function with_errors($container)
162
	{
163 164 165
		$errors = ($container instanceof Validator) ? $container->errors : $container;

		return $this->with('errors', $errors);
166 167
	}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	/**
	 * 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();
	}

187
}