lithium\action\Controller::redirect()
Creates a redirect response by calling render() and providing a 'location' parameter.
Parameters
-
mixed
$urlThe location to redirect to, provided as a string relative to the root of the application, a fully-qualified URL, or an array of routing parameters to be resolved to a URL. Post-processed by
Router::match(). -
array
$optionsOptions when performing the redirect. Available options include:
'status'integer: The HTTP status code associated with the redirect. Defaults to302.'head'boolean: Determines whether only headers are returned with the response. Defaults totrue, in which case only headers and no body are returned. Set tofalseto render a body as well.'exit'boolean: Exit immediately after rendering. Defaults tofalse. Becauseredirect()does not exit by default, you should always prefix calls with areturnstatement, so that the action is always immediately exited.
Returns
objectReturns the instance of the Response object associated with this controller.
Filter
Allows to intercept redirects, either stopping them completely i.e. during debugging or for logging purposes.
Source
public function redirect($url, array $options = []) {
$defaults = ['location' => null, 'status' => 302, 'head' => true, 'exit' => false];
$options += $defaults;
$params = compact('url', 'options');
Filters::run($this, __FUNCTION__, $params, function($params) {
$router = $this->_classes['router'];
$options = $params['options'];
$location = $options['location'] ?: $router::match($params['url'], $this->request);
$this->render(compact('location') + $options);
});
if ($options['exit']) {
$this->response->render();
$this->_stop();
}
return $this->response;
}