/**
  * @param CanonicalUrlEvent $event
  */
 public function generateUrlCanonical(CanonicalUrlEvent $event)
 {
     if ($event->getUrl() !== null) {
         return;
     }
     $parseUrlByCurrentLocale = $this->getParsedUrlByCurrentLocale();
     if (empty($parseUrlByCurrentLocale['host'])) {
         return;
     }
     // Be sure to use the proper domain name
     $canonicalUrl = $parseUrlByCurrentLocale['scheme'] . '://' . $parseUrlByCurrentLocale['host'];
     // preserving a potential subdirectory, e.g. http://somehost.com/mydir/index.php/...
     $canonicalUrl .= $this->request->getBaseUrl();
     // Remove script name from path, e.g. http://somehost.com/index.php/...
     $canonicalUrl = preg_replace("!/index(_dev)?\\.php!", '', $canonicalUrl);
     $path = $this->request->getPathInfo();
     if (!empty($path) && $path != "/") {
         $canonicalUrl .= $path;
         $canonicalUrl = rtrim($canonicalUrl, '/');
     } else {
         $queryString = $this->request->getQueryString();
         if (!empty($queryString)) {
             $canonicalUrl .= '/?' . $queryString;
         }
     }
     $event->setUrl($canonicalUrl);
 }
 /**
  * @param CanonicalUrlEvent $event
  */
 public function generateUrlCanonical(CanonicalUrlEvent $event)
 {
     $preUrl = null;
     $url = null;
     $uri = $this->request->getUri();
     $parseUrlByCurrentLocale = $this->getParseUrlByCurrentLocale();
     if (!empty($uri) && false !== ($parse = parse_url($uri))) {
         // test if current domain equal lang domain
         if ($parse['host'] !== $parseUrlByCurrentLocale['host']) {
             $preUrl = $parseUrlByCurrentLocale['scheme'] . '://' . $parseUrlByCurrentLocale['host'];
         }
         if (strpos($parse['path'], '/index.php') > -1) {
             $path = explode('/index.php', $parse['path']);
             $url = $path[1];
         } elseif (strpos($parse['path'], '/index_dev.php') > -1) {
             $path = explode('/index_dev.php', $parse['path']);
             $url = $path[1];
         } elseif ($parse['path'] !== '/') {
             $url = $parse['path'];
         }
     }
     if (empty($url)) {
         $url = '/?' . $this->request->getQueryString();
     }
     $event->setUrl($preUrl . $url);
 }
 public function testOverrideCanonicalEvent()
 {
     $canonicalUrlListener = new CanonicalUrlListener(Request::create('https://myhost.com/test'));
     $event = new CanonicalUrlEvent();
     // override canonical
     $canonical = 'http://myscanonical.com';
     $event->setUrl($canonical);
     $canonicalUrlListener->generateUrlCanonical($event);
     $this->assertEquals($canonical, $event->getUrl());
 }