lithium\action\Controller::redirect PHP Method

redirect() public method

Creates a redirect response by calling render() and providing a 'location' parameter.
See also: lithium\net\http\Router::match()
See also: lithium\action\Controller::$response
public redirect ( mixed $url, array $options = [] ) : object
$url mixed The 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()`.
$options array Options when performing the redirect. Available options include: - `'status'` _integer_: The HTTP status code associated with the redirect. Defaults to `302`. - `'head'` _boolean_: Determines whether only headers are returned with the response. Defaults to `true`, in which case only headers and no body are returned. Set to `false` to render a body as well. - `'exit'` _boolean_: Exit immediately after rendering. Defaults to `false`. Because `redirect()` does not exit by default, you should always prefix calls with a `return` statement, so that the action is always immediately exited.
return object Returns the instance of the `Response` object associated with this controller.
    public function redirect($url, array $options = array())
    {
        $router = $this->_classes['router'];
        $defaults = array('location' => null, 'status' => 302, 'head' => true, 'exit' => false);
        $options += $defaults;
        $params = compact('url', 'options');
        $this->_filter(__METHOD__, $params, function ($self, $params) use($router) {
            $options = $params['options'];
            $location = $options['location'] ?: $router::match($params['url'], $self->request);
            $self->render(compact('location') + $options);
        });
        if ($options['exit']) {
            $this->response->render();
            $this->_stop();
        }
        return $this->response;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * automatic supplement of library for redirects
  *
  * @see lithium\net\http\Router::match()
  * @see lithium\action\Controller::$response
  * @see lithium\action\Controller::redirect()
  * @param mixed $url The 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()`.
  * @param array $options Options when performing the redirect. Available options include:
  *              - `'status'` _integer_: The HTTP status code associated with the redirect.
  *                Defaults to `302`.
  *              - `'head'` _boolean_: Determines whether only headers are returned with the
  *                response. Defaults to `true`, in which case only headers and no body are
  *                returned. Set to `false` to render a body as well.
  *              - `'exit'` _boolean_: Exit immediately after rendering. Defaults to `false`.
  *                Because `redirect()` does not exit by default, you should always prefix calls
  *                with a `return` statement, so that the action is always immediately exited.
  * @return object Returns the instance of the `Response` object associated with this controller.
  */
 public function redirect($url, array $options = array())
 {
     return parent::redirect($this->_url($url), $options);
 }