Prado\Web\TUrlManager::constructUrl PHP Méthode

constructUrl() public méthode

This method provides the actual implementation used by {@link THttpRequest::constructUrl}. Override this method if you want to provide your own way of URL formatting. If you do so, you may also need to override {@link parseUrl} so that the URL can be properly parsed. The URL is constructed as the following format: entryscript.php?serviceID=serviceParameter&get1=value1&... If {@link THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'Path', the following format is used instead: entryscript.php/serviceID/serviceParameter/get1,value1/get2,value2... If {@link THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'HiddenPath', then entryscript.php will be hidden and the following format is used instead: serviceID/serviceParameter/get1,value1/get2,value2... In order to use the 'HiddenPath' format you need proper url rewrite configuration; here's an example for Apache's .htaccess: Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [L]
See also: parseUrl
public constructUrl ( $serviceID, $serviceParam, $getItems, $encodeAmpersand, $encodeGetItems ) : string
Résultat string URL
    public function constructUrl($serviceID, $serviceParam, $getItems, $encodeAmpersand, $encodeGetItems)
    {
        $url = $serviceID . '=' . urlencode($serviceParam);
        $amp = $encodeAmpersand ? '&' : '&';
        $request = $this->getRequest();
        if (is_array($getItems) || $getItems instanceof \Traversable) {
            if ($encodeGetItems) {
                foreach ($getItems as $name => $value) {
                    if (is_array($value)) {
                        $name = urlencode($name . '[]');
                        foreach ($value as $v) {
                            $url .= $amp . $name . '=' . urlencode($v);
                        }
                    } else {
                        $url .= $amp . urlencode($name) . '=' . urlencode($value);
                    }
                }
            } else {
                foreach ($getItems as $name => $value) {
                    if (is_array($value)) {
                        foreach ($value as $v) {
                            $url .= $amp . $name . '[]=' . $v;
                        }
                    } else {
                        $url .= $amp . $name . '=' . $value;
                    }
                }
            }
        }
        switch ($request->getUrlFormat()) {
            case THttpRequestUrlFormat::Path:
                return $request->getApplicationUrl() . '/' . strtr($url, array($amp => '/', '?' => '/', '=' => $request->getUrlParamSeparator()));
            case THttpRequestUrlFormat::HiddenPath:
                return rtrim(dirname($request->getApplicationUrl()), '/') . '/' . strtr($url, array($amp => '/', '?' => '/', '=' => $request->getUrlParamSeparator()));
            default:
                return $request->getApplicationUrl() . '?' . $url;
        }
    }

Usage Example

Exemple #1
0
 /**
  * Constructs a URL that can be recognized by PRADO.
  *
  * This method provides the actual implementation used by {@link THttpRequest::constructUrl}.
  * Override this method if you want to provide your own way of URL formatting.
  * If you do so, you may also need to override {@link parseUrl} so that the URL can be properly parsed.
  *
  * The URL is constructed as the following format:
  * /entryscript.php?serviceID=serviceParameter&get1=value1&...
  * If {@link THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'Path',
  * the following format is used instead:
  * /entryscript.php/serviceID/serviceParameter/get1,value1/get2,value2...
  * If {@link THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'HiddenPath',
  * the following format is used instead:
  * /serviceID/serviceParameter/get1,value1/get2,value2...
  * @param string service ID
  * @param string service parameter
  * @param array GET parameters, null if not provided
  * @param boolean whether to encode the ampersand in URL
  * @param boolean whether to encode the GET parameters (their names and values)
  * @return string URL
  * @see parseUrl
  * @since 3.1.1
  */
 public function constructUrl($serviceID, $serviceParam, $getItems, $encodeAmpersand, $encodeGetItems)
 {
     if ($this->_customUrl) {
         if (!(is_array($getItems) || $getItems instanceof \Traversable)) {
             $getItems = array();
         }
         $key = $serviceID . ':' . $serviceParam;
         $wildCardKey = ($pos = strrpos($serviceParam, '.')) !== false ? $serviceID . ':' . substr($serviceParam, 0, $pos) . '.*' : $serviceID . ':*';
         if (isset($this->_constructRules[$key])) {
             foreach ($this->_constructRules[$key] as $rule) {
                 if ($rule->supportCustomUrl($getItems)) {
                     return $rule->constructUrl($getItems, $encodeAmpersand, $encodeGetItems);
                 }
             }
         } elseif (isset($this->_constructRules[$wildCardKey])) {
             foreach ($this->_constructRules[$wildCardKey] as $rule) {
                 if ($rule->supportCustomUrl($getItems)) {
                     $getItems['*'] = $pos ? substr($serviceParam, $pos + 1) : $serviceParam;
                     return $rule->constructUrl($getItems, $encodeAmpersand, $encodeGetItems);
                 }
             }
         }
     }
     return parent::constructUrl($serviceID, $serviceParam, $getItems, $encodeAmpersand, $encodeGetItems);
 }