Base::reroute PHP Method

reroute() public method

Reroute to specified URI
public reroute ( $url = NULL, $permanent = FALSE ) : null
$url array|string
$permanent bool
return null
    function reroute($url = NULL, $permanent = FALSE)
    {
        if (!$url) {
            $url = $this->hive['REALM'];
        }
        if (is_array($url)) {
            $url = call_user_func_array([$this, 'alias'], $url);
        } elseif (preg_match('/^(?:@(\\w+)(?:(\\(.+?)\\))*(\\?.+)*)/', $url, $parts)) {
            if (empty($this->hive['ALIASES'][$parts[1]])) {
                user_error(sprintf(self::E_Named, $parts[1]), E_USER_ERROR);
            }
            $url = $this->hive['ALIASES'][$parts[1]];
        }
        $url = $this->build($url, isset($parts[2]) ? $this->parse($parts[2]) : []) . (isset($parts[3]) ? $parts[3] : '');
        if (($handler = $this->hive['ONREROUTE']) && $this->call($handler, [$url, $permanent]) !== FALSE) {
            return;
        }
        if ($url[0] == '/') {
            $port = $this->hive['PORT'];
            $port = in_array($port, [80, 443]) ? '' : ':' . $port;
            $url = $this->hive['SCHEME'] . '://' . $this->hive['HOST'] . $port . $this->hive['BASE'] . $url;
        }
        if (!$this->hive['CLI']) {
            header('Location: ' . $url);
            $this->status($permanent ? 301 : 302);
            die;
        }
        $this->mock('GET ' . $url);
    }

Usage Example

コード例 #1
0
ファイル: middleware.php プロジェクト: Kekesed/Kambeng-Blog
 public function run($event = 'before')
 {
     if (!isset($this->routes[$event])) {
         return true;
     }
     foreach ($keys = array_keys($this->routes[$event]) as $key) {
         $paths[] = str_replace('@', '*@', $key);
     }
     $vals = array_values($this->routes[$event]);
     array_multisort($paths, SORT_DESC, $keys, $vals);
     $this->routes[$event] = array_combine($keys, $vals);
     // Convert to BASE-relative URL
     $req = $this->f3->rel(urldecode($this->f3->URI));
     foreach ($this->routes[$event] as $pattern => $routes) {
         if (!($args = $this->f3->mask($pattern, $req))) {
             continue;
         }
         ksort($args);
         $route = NULL;
         if (isset($routes[$ptr = $this->f3->AJAX + 1][$this->f3->VERB])) {
             $route = $routes[$ptr];
         } elseif (isset($routes[\Base::REQ_SYNC | \Base::REQ_AJAX])) {
             $route = $routes[\Base::REQ_SYNC | \Base::REQ_AJAX];
         }
         if (!$route) {
             continue;
         }
         if ($this->f3->VERB != 'OPTIONS' && isset($route[$this->f3->VERB])) {
             $parts = parse_url($req);
             if ($this->f3->VERB == 'GET' && preg_match('/.+\\/$/', $parts['path'])) {
                 $this->f3->reroute(substr($parts['path'], 0, -1) . (isset($parts['query']) ? '?' . $parts['query'] : ''));
             }
             $handler = $route[$this->f3->VERB][0];
             if (is_bool(strpos($pattern, '/*'))) {
                 foreach (array_keys($args) as $key) {
                     if (is_numeric($key) && $key) {
                         unset($args[$key]);
                     }
                 }
             }
             if (is_string($handler)) {
                 // Replace route pattern tokens in handler if any
                 $handler = preg_replace_callback('/@(\\w+\\b)/', function ($id) use($args) {
                     return isset($args[$id[1]]) ? $args[$id[1]] : $id[0];
                 }, $handler);
                 if (preg_match('/(.+)\\h*(?:->|::)/', $handler, $match) && !class_exists($match[1])) {
                     $this->f3->error(500, 'PreRoute handler not found');
                 }
             }
             // Call route handler
             return $this->f3->call($handler, array($this->f3, $args), 'beforeroute,afterroute') !== FALSE;
         }
     }
     return true;
 }
All Usage Examples Of Base::reroute