Nette\Application\Routers\Route::match PHP Method

match() public method

Maps HTTP request to a Request object.
public match ( Nette\Http\IRequest $httpRequest ) : Nette\Application\Request | null
$httpRequest Nette\Http\IRequest
return Nette\Application\Request | null
    public function match(Nette\Http\IRequest $httpRequest)
    {
        // combine with precedence: mask (params in URL-path), fixity, query, (post,) defaults
        // 1) URL MASK
        $url = $httpRequest->getUrl();
        $re = $this->re;
        if ($this->type === self::HOST) {
            $host = $url->getHost();
            $path = '//' . $host . $url->getPath();
            $parts = ip2long($host) ? [$host] : array_reverse(explode('.', $host));
            $re = strtr($re, ['/%basePath%/' => preg_quote($url->getBasePath(), '#'), '%tld%' => preg_quote($parts[0], '#'), '%domain%' => preg_quote(isset($parts[1]) ? "{$parts['1']}.{$parts['0']}" : $parts[0], '#'), '%sld%' => preg_quote(isset($parts[1]) ? $parts[1] : '', '#'), '%host%' => preg_quote($host, '#')]);
        } elseif ($this->type === self::RELATIVE) {
            $basePath = $url->getBasePath();
            if (strncmp($url->getPath(), $basePath, strlen($basePath)) !== 0) {
                return NULL;
            }
            $path = (string) substr($url->getPath(), strlen($basePath));
        } else {
            $path = $url->getPath();
        }
        if ($path !== '') {
            $path = rtrim(rawurldecode($path), '/') . '/';
        }
        if (!($matches = Strings::match($path, $re))) {
            // stop, not matched
            return NULL;
        }
        // assigns matched values to parameters
        $params = [];
        foreach ($matches as $k => $v) {
            if (is_string($k) && $v !== '') {
                $params[$this->aliases[$k]] = $v;
            }
        }
        // 2) CONSTANT FIXITY
        foreach ($this->metadata as $name => $meta) {
            if (!isset($params[$name]) && isset($meta['fixity']) && $meta['fixity'] !== self::OPTIONAL) {
                $params[$name] = NULL;
                // cannot be overwriten in 3) and detected by isset() in 4)
            }
        }
        // 3) QUERY
        if ($this->xlat) {
            $params += self::renameKeys($httpRequest->getQuery(), array_flip($this->xlat));
        } else {
            $params += $httpRequest->getQuery();
        }
        // 4) APPLY FILTERS & FIXITY
        foreach ($this->metadata as $name => $meta) {
            if (isset($params[$name])) {
                if (!is_scalar($params[$name])) {
                } elseif (isset($meta[self::FILTER_TABLE][$params[$name]])) {
                    // applies filterTable only to scalar parameters
                    $params[$name] = $meta[self::FILTER_TABLE][$params[$name]];
                } elseif (isset($meta[self::FILTER_TABLE]) && !empty($meta[self::FILTER_STRICT])) {
                    return NULL;
                    // rejected by filterTable
                } elseif (isset($meta[self::FILTER_IN])) {
                    // applies filterIn only to scalar parameters
                    $params[$name] = call_user_func($meta[self::FILTER_IN], (string) $params[$name]);
                    if ($params[$name] === NULL && !isset($meta['fixity'])) {
                        return NULL;
                        // rejected by filter
                    }
                }
            } elseif (isset($meta['fixity'])) {
                $params[$name] = $meta[self::VALUE];
            }
        }
        if (isset($this->metadata[NULL][self::FILTER_IN])) {
            $params = call_user_func($this->metadata[NULL][self::FILTER_IN], $params);
            if ($params === NULL) {
                return NULL;
            }
        }
        // 5) BUILD Request
        if (!isset($params[self::PRESENTER_KEY])) {
            throw new Nette\InvalidStateException('Missing presenter in route definition.');
        } elseif (!is_string($params[self::PRESENTER_KEY])) {
            return NULL;
        }
        $presenter = $params[self::PRESENTER_KEY];
        unset($params[self::PRESENTER_KEY]);
        if (isset($this->metadata[self::MODULE_KEY])) {
            $presenter = (isset($params[self::MODULE_KEY]) ? $params[self::MODULE_KEY] . ':' : '') . $presenter;
            unset($params[self::MODULE_KEY]);
        }
        return new Application\Request($presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), [Application\Request::SECURED => $httpRequest->isSecured()]);
    }

Usage Example

Esempio n. 1
0
 /**
  * @param Nette\Web\IHttpRequest $httpRequest
  * @return Nette\Application\PresenterRequest|NULL
  */
 public function match(\Nette\Http\IRequest $httpRequest)
 {
     /** @var $appRequest \Nette\Application\Request */
     $appRequest = parent::match($httpRequest);
     // doplněno: pokud match vrátí NULL, musíme také vrátit NULL
     if (!$appRequest) {
         return $appRequest;
     }
     // musím si přeložit host, kvůli localhostu to není dané
     if (strpos($httpRequest->url->host, '.localhost') !== false && !\Nette\Environment::isProduction()) {
         // jsme na localhostu
         $host = substr($httpRequest->url->host, 0, strripos($httpRequest->url->host, '.localhost'));
         // musíme si doménu osamostatnit od .localhost
         $host = str_replace('_', '.', $host);
         // na lokálu mám místo teček podtržení
     } else {
         // jsme na produkci
         $host = $httpRequest->url->host;
     }
     // zkusím zda je soubor již v cache
     if (is_file(WEB_DIR . '/' . $host . '/cache' . $httpRequest->url->path)) {
         return new Nette\Application\Request('Frontend', 'POST', array('action' => 'cache', 'file' => WEB_DIR . '/' . $host . '/cache' . $httpRequest->url->path));
     }
     /*
     if ($params = $this->doFilterParams($this->getRequestParams($appRequest), $appRequest, self::WAY_IN)) {
     	return $this->setRequestParams($appRequest, $params);
     }
     */
     // $pages = $this->context->createPages();
     $data = array('action' => 'default');
     return new Nette\Application\Request('Frontend', 'POST', $data);
 }
All Usage Examples Of Nette\Application\Routers\Route::match