示例#1
0
 /**
  * @throws \RunTimeException
  * @param null|\Sonata\PageBundle\Model\PageInterface|string $page
  * @param bool $absolute
  * @return string
  */
 public function url($page = null, $absolute = false)
 {
     if (!$page) {
         return '';
     }
     $context = $this->router->getContext();
     if ($page instanceof PageInterface) {
         if ($page->isDynamic()) {
             if ($this->environment->isDebug()) {
                 throw new \RunTimeException('Unable to generate path for dynamic page');
             }
             return '';
         }
         $url = $page->getCustomUrl() ?: $page->getUrl();
     } else {
         $url = $page;
     }
     $url = sprintf('%s%s', $context->getBaseUrl(), $url);
     if ($absolute && $context->getHost()) {
         $scheme = $context->getScheme();
         $port = '';
         if ('http' === $scheme && 80 != $context->getHttpPort()) {
             $port = ':' . $context->getHttpPort();
         } elseif ('https' === $scheme && 443 != $context->getHttpsPort()) {
             $port = ':' . $context->getHttpsPort();
         }
         $url = $scheme . '://' . $context->getHost() . $port . $url;
     }
     return $url;
 }