Ejemplo n.º 1
0
 /**
  * Sets a header to redirect to a given URI or URL.
  * @method redirect
  * @static
  * @param {string} $uri The URL or internal URI to redirect to
  * @param {array} $options An array of options that can include:
  *  "loop" => Defaults to false. If true, sets the redirect header even if the current URL is the same.
  *  "noProxy" => Defaults to false. If true, doesn't use the proxy mapping to determine URL
  *  "permanently" => If true, sets response code as 304 instead of 302
  * @param {boolean} [$noProxy=false]
  * @return {boolean}
  *  Return whether the redirect header was set.
  */
 static function redirect($uri, $options = array())
 {
     extract($options);
     $url = Q_Uri::url($uri, null, !empty($noProxy));
     if ($url === Q_Uri::unreachableUri()) {
         throw new Q_Exception_BadValue(array('internal' => 'uri', 'problem' => 'no url routes to it'));
     }
     $level = ob_get_level();
     for ($i = 0; $i < $level; ++$i) {
         ob_clean();
     }
     /**
      * @event Q/response {before}
      * @param {string} permanently
      * @param {string} uri
      * @param {string} url
      * @param {string} loop
      * @return {boolean}
      */
     $result = Q::event('Q/redirect', compact('uri', 'url', 'loop', 'permanently', 'noProxy', 'level'), 'before');
     if (isset($result)) {
         return $result;
     }
     if (!empty($loop) and Q_Request::url() === $url) {
         return false;
     }
     if (!Q_Request::isAjax()) {
         if (!empty($permanently)) {
             header("HTTP/1.1 301 Moved Permanently");
         }
         header("Location: {$url}");
     }
     self::$redirected = $uri;
     return true;
 }