Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function generateFromRoute($name, $parameters = array(), $options = array(), $collect_bubbleable_metadata = FALSE)
 {
     $options += array('prefix' => '');
     $route = $this->getRoute($name);
     $generated_url = $collect_bubbleable_metadata ? new GeneratedUrl() : NULL;
     $query_params = [];
     // Symfony adds any parameters that are not path slugs as query strings.
     if (isset($options['query']) && is_array($options['query'])) {
         $query_params = $options['query'];
     }
     $fragment = '';
     if (isset($options['fragment'])) {
         if (($fragment = trim($options['fragment'])) != '') {
             $fragment = '#' . $fragment;
         }
     }
     // Generate a relative URL having no path, just query string and fragment.
     if ($route->getOption('_no_path')) {
         $query = $query_params ? '?' . http_build_query($query_params, '', '&') : '';
         $url = $query . $fragment;
         return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
     }
     $options += $route->getOption('default_url_options') ?: [];
     $options += array('prefix' => '', 'path_processing' => TRUE);
     $name = $this->getRouteDebugMessage($name);
     $this->processRoute($name, $route, $parameters, $generated_url);
     $path = $this->getInternalPathFromRoute($name, $route, $parameters, $query_params);
     // Outbound path processors might need the route object for the path, e.g.
     // to get the path pattern.
     $options['route'] = $route;
     if ($options['path_processing']) {
         $path = $this->processPath($path, $options, $generated_url);
     }
     if (!empty($options['prefix'])) {
         $path = ltrim($path, '/');
         $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];
         $path = '/' . str_replace('%2F', '/', rawurlencode($prefix)) . $path;
     }
     // The base_url might be rewritten from the language rewrite in domain mode.
     if (isset($options['base_url'])) {
         $base_url = $options['base_url'];
         if (isset($options['https'])) {
             if ($options['https'] === TRUE) {
                 $base_url = str_replace('http://', 'https://', $base_url);
             } elseif ($options['https'] === FALSE) {
                 $base_url = str_replace('https://', 'http://', $base_url);
             }
         }
         $url = $base_url . $path . $fragment;
         return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
     }
     $base_url = $this->context->getBaseUrl();
     $absolute = !empty($options['absolute']);
     if (!$absolute || !($host = $this->context->getHost())) {
         $url = $base_url . $path . $fragment;
         return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
     }
     // Prepare an absolute URL by getting the correct scheme, host and port from
     // the request context.
     if (isset($options['https'])) {
         $scheme = $options['https'] ? 'https' : 'http';
     } else {
         $scheme = $this->context->getScheme();
     }
     $scheme_req = $route->getSchemes();
     if ($scheme_req && ($req = $scheme_req[0]) && $scheme !== $req) {
         $scheme = $req;
     }
     $port = '';
     if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
         $port = ':' . $this->context->getHttpPort();
     } elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
         $port = ':' . $this->context->getHttpsPort();
     }
     if ($collect_bubbleable_metadata) {
         $generated_url->addCacheContexts(['url.site']);
     }
     $url = $scheme . '://' . $host . $port . $base_url . $path . $fragment;
     return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function generateFromRoute($name, $parameters = array(), $options = array(), $collect_cacheability_metadata = FALSE)
 {
     $generated_url = $collect_cacheability_metadata ? new GeneratedUrl() : NULL;
     $options += array('prefix' => '');
     $route = $this->getRoute($name);
     $name = $this->getRouteDebugMessage($name);
     $this->processRoute($name, $route, $parameters, $generated_url);
     $query_params = [];
     // Symfony adds any parameters that are not path slugs as query strings.
     if (isset($options['query']) && is_array($options['query'])) {
         $query_params = $options['query'];
     }
     $path = $this->getInternalPathFromRoute($name, $route, $parameters, $query_params);
     $path = $this->processPath($path, $options, $generated_url);
     if (!empty($options['prefix'])) {
         $path = ltrim($path, '/');
         $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];
         $path = '/' . str_replace('%2F', '/', rawurlencode($prefix)) . $path;
     }
     $fragment = '';
     if (isset($options['fragment'])) {
         if (($fragment = trim($options['fragment'])) != '') {
             $fragment = '#' . $fragment;
         }
     }
     // The base_url might be rewritten from the language rewrite in domain mode.
     if (isset($options['base_url'])) {
         $base_url = $options['base_url'];
         if (isset($options['https'])) {
             if ($options['https'] === TRUE) {
                 $base_url = str_replace('http://', 'https://', $base_url);
             } elseif ($options['https'] === FALSE) {
                 $base_url = str_replace('https://', 'http://', $base_url);
             }
         }
         $url = $base_url . $path . $fragment;
         return $collect_cacheability_metadata ? $generated_url->setGeneratedUrl($url) : $url;
     }
     $base_url = $this->context->getBaseUrl();
     $absolute = !empty($options['absolute']);
     if (!$absolute || !($host = $this->context->getHost())) {
         if ($route->getOption('_only_fragment')) {
             return $collect_cacheability_metadata ? $generated_url->setGeneratedUrl($fragment) : $fragment;
         }
         $url = $base_url . $path . $fragment;
         return $collect_cacheability_metadata ? $generated_url->setGeneratedUrl($url) : $url;
     }
     // Prepare an absolute URL by getting the correct scheme, host and port from
     // the request context.
     if (isset($options['https'])) {
         $scheme = $options['https'] ? 'https' : 'http';
     } else {
         $scheme = $this->context->getScheme();
     }
     $scheme_req = $route->getRequirement('_scheme');
     if (isset($scheme_req) && ($req = strtolower($scheme_req)) && $scheme !== $req) {
         $scheme = $req;
     }
     $port = '';
     if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
         $port = ':' . $this->context->getHttpPort();
     } elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
         $port = ':' . $this->context->getHttpsPort();
     }
     if ($collect_cacheability_metadata) {
         $generated_url->addCacheContexts(['url.site']);
     }
     $url = $scheme . '://' . $host . $port . $base_url . $path . $fragment;
     return $collect_cacheability_metadata ? $generated_url->setGeneratedUrl($url) : $url;
 }