redirect.php 3.73 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2

Taylor Otwell committed
3
class Redirect extends Response {
4 5

	/**
6
	 * Create a redirect response.
7
	 *
8 9 10 11
	 * <code>
	 *		// Create a redirect response to a location within the application
	 *		return Redirect::to('user/profile');
	 *
12
	 *		// Create a redirect response with a 301 status code
13 14 15
	 *		return Redirect::to('user/profile', 301);
	 * </code>
	 *
16 17 18
	 * @param  string    $url
	 * @param  int       $status
	 * @param  bool      $https
19
	 * @return Redirect
20
	 */
21
	public static function to($url, $status = 302, $https = false)
22
	{
23
		return static::make('', $status)->header('Location', URL::to($url, $https));
24 25 26
	}

	/**
27 28 29 30 31 32
	 * Create a redirect response to a HTTPS URL.
	 *
	 * @param  string    $url
	 * @param  int       $status
	 * @return Response
	 */
33
	public static function to_secure($url, $status = 302)
34
	{
35
		return static::to($url, $status, true);
36 37 38
	}

	/**
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	 * 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
	 * @param  bool      $https
	 * @return Redirect
	 */
	public static function to_route($route, $parameters = array(), $status = 302, $https = false)
	{
		return static::to(URL::to_route($route, $parameters, $https), $status);
	}

	/**
	 * Create a redirect response to a named route using HTTPS.
	 *
	 * @param  string    $route
	 * @param  array     $parameters
	 * @param  int       $status
	 * @return Redirect
	 */
	public static function to_secure_route($route, $parameters = array(), $status = 302)
	{
		return static::to_route($route, $parameters, $status, true);
	}

	/**
74 75
	 * Add an item to the session flash data.
	 *
76
	 * This is useful for "passing" status messages or other data to the next request.
77
	 *
78
	 * <code>
79 80
	 *		// Create a redirect response and flash to the session
	 *		return Redirect::to('profile')->with('message', 'Welcome Back!');
81 82
	 * </code>
	 *
83 84
	 * @param  string          $key
	 * @param  mixed           $value
85 86
	 * @return Response
	 */
87
	public function with($key, $value)
88
	{
89
		if (Config::get('session.driver') == '')
90
		{
91
			throw new \Exception('A session driver must be set before setting flash data.');
92 93
		}

94
		Session::flash($key, $value);
95

96
		return $this;
97 98 99
	}

	/**
100 101 102 103 104 105
	 * 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
106
	 *		return Redirect::to('login')->with_input();
107 108
	 *
	 *		// Redirect and flash only a few of the input items
109
	 *		return Redirect::to('login')->with_input('only', array('email', 'username'));
110 111
	 *
	 *		// Redirect and flash all but a few of the input items
112
	 *		return Redirect::to('login')->with_input('except', array('password', 'ssn'));
113 114 115 116 117 118 119 120 121
	 * </code>
	 *
	 * @param  string    $filter
	 * @param  array     $items
	 * @return Redirect
	 */
	public function with_input($filter = null, $items = array())
	{
		Input::flash($filter, $items);
122

123 124 125 126
		return $this;
	}

	/**
127 128 129 130 131
	 * Flash a Validator's errors to the session data.
	 *
	 * This method allows you to conveniently pass validation errors back to views.
	 *
	 * <code>
132
	 *		// Redirect and flash validator errors the session
133 134 135
	 *		return Redirect::to('register')->with_errors($validator);
	 * </code>
	 *
136
	 * @param  Validator|Messages  $container
137 138
	 * @return Redirect
	 */
139
	public function with_errors($container)
140
	{
141 142 143
		$errors = ($container instanceof Validator) ? $container->errors : $container;

		return $this->with('errors', $errors);
144 145 146
	}

}