1: <?php namespace Laravel; use Laravel\Routing\Router;
2:
3: class Redirect extends Response {
4:
5: /**
6: * Create a redirect response to application root.
7: *
8: * @param int $status
9: * @param bool $https
10: * @return Redirect
11: */
12: public static function home($status = 302, $https = null)
13: {
14: return static::to(URL::home($https), $status);
15: }
16:
17: /**
18: * Create a redirect response to the HTTP referrer.
19: *
20: * @param int $status
21: * @return Redirect
22: */
23: public static function back($status = 302)
24: {
25: return static::to(Request::referrer(), $status);
26: }
27:
28: /**
29: * Create a redirect response.
30: *
31: * <code>
32: * // Create a redirect response to a location within the application
33: * return Redirect::to('user/profile');
34: *
35: * // Create a redirect response with a 301 status code
36: * return Redirect::to('user/profile', 301);
37: * </code>
38: *
39: * @param string $url
40: * @param int $status
41: * @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: * Create a redirect response to a HTTPS URL.
51: *
52: * @param string $url
53: * @param int $status
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: * Create a redirect response to a controller action.
63: *
64: * @param string $action
65: * @param array $parameters
66: * @param int $status
67: * @return Redirect
68: */
69: 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: * Create a redirect response to a named route.
76: *
77: * <code>
78: * // Create a redirect response to the "login" named route
79: * return Redirect::to_route('login');
80: *
81: * // Create a redirect response to the "profile" named route with parameters
82: * return Redirect::to_route('profile', array($username));
83: * </code>
84: *
85: * @param string $route
86: * @param array $parameters
87: * @param int $status
88: * @return Redirect
89: */
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: * Add an item to the session flash data.
97: *
98: * This is useful for "passing" status messages or other data to the next request.
99: *
100: * <code>
101: * // Create a redirect response and flash to the session
102: * return Redirect::to('profile')->with('message', 'Welcome Back!');
103: * </code>
104: *
105: * @param string $key
106: * @param mixed $value
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: * Flash the old input to the session and return the Redirect instance.
123: *
124: * Once the input has been flashed, it can be retrieved via the Input::old method.
125: *
126: * <code>
127: * // 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: * </code>
136: *
137: * @param string $filter
138: * @param array $items
139: * @return Redirect
140: */
141: public function with_input($filter = null, $items = array())
142: {
143: Input::flash($filter, $items);
144:
145: return $this;
146: }
147:
148: /**
149: * Flash a Validator's errors to the session data.
150: *
151: * This method allows you to conveniently pass validation errors back to views.
152: *
153: * <code>
154: * // Redirect and flash validator errors the session
155: * return Redirect::to('register')->with_errors($validator);
156: * </code>
157: *
158: * @param Validator|Messages $container
159: * @return Redirect
160: */
161: public function with_errors($container)
162: {
163: $errors = ($container instanceof Validator) ? $container->errors : $container;
164:
165: return $this->with('errors', $errors);
166: }
167:
168: /**
169: * Send the headers and content of the response to the browser.
170: *
171: * @return void
172: */
173: public function send()
174: {
175: // Dump all output buffering, this ensures
176: // that symphony will send our redirect headers
177: // properly if we've outputted any content from
178: // within Laravel.
179: while (ob_get_level() > 0)
180: {
181: ob_end_clean();
182: }
183:
184: return parent::send();
185: }
186:
187: }