From 28aea640aac9ddc263b97537871f8f09cf292fa8 Mon Sep 17 00:00:00 2001 From: Luis Cordeiro Date: Sun, 6 Dec 2020 15:13:59 +0000 Subject: [PATCH 1/3] Update UriLocalizer.php Add support for base url. --- src/UriLocalizer.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/UriLocalizer.php b/src/UriLocalizer.php index de303a7..6b96916 100644 --- a/src/UriLocalizer.php +++ b/src/UriLocalizer.php @@ -25,6 +25,7 @@ public function __construct(LanguageRepository $languageRepository, Request $req public function localeFromRequest($segment = 0) { $url = $this->request->getUri(); + $url = $this->removeBaseUrl($url); return $this->getLocaleFromUrl($url, $segment); } @@ -40,6 +41,7 @@ public function localeFromRequest($segment = 0) */ public function localize($url, $locale, $segment = 0) { + $url = $this->removeBaseUrl($url); $cleanUrl = $this->cleanUrl($url, $segment); $parsedUrl = $this->parseUrl($cleanUrl, $segment); @@ -59,6 +61,7 @@ public function localize($url, $locale, $segment = 0) */ public function getLocaleFromUrl($url, $segment = 0) { + $url = $this->removeBaseUrl($url); return $this->parseUrl($url, $segment)['locale']; } @@ -72,6 +75,7 @@ public function getLocaleFromUrl($url, $segment = 0) */ public function cleanUrl($url, $segment = 0) { + $url = $this->removeBaseUrl($url); $parsedUrl = $this->parseUrl($url, $segment); // Remove locale from segments: if ($parsedUrl['locale']) { @@ -141,4 +145,14 @@ protected function removeTrailingSlash($path) { return strlen($path) > 0 && substr($path, -1) === '/' ? substr($path, 0, -1) : $path; } + + /** + * Remove base URL from URL string + * + * @param string $url + * @return string + */ + protected function removeBaseUrl($url) { + return str_ireplace(\URL::to('/'), "", $url); + } } From f492fe0c8a55e77263330a77fe19566c3b56cb5a Mon Sep 17 00:00:00 2001 From: Luis Cordeiro Date: Sun, 6 Dec 2020 19:10:01 +0000 Subject: [PATCH 2/3] Add protection for empty URL --- src/UriLocalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UriLocalizer.php b/src/UriLocalizer.php index 6b96916..c9cc96b 100644 --- a/src/UriLocalizer.php +++ b/src/UriLocalizer.php @@ -153,6 +153,6 @@ protected function removeTrailingSlash($path) * @return string */ protected function removeBaseUrl($url) { - return str_ireplace(\URL::to('/'), "", $url); + return str_ireplace(\URL::to('/'), "", $url) ?: '/'; } } From 8fc477f0c5633e425459810a297a20658b77786a Mon Sep 17 00:00:00 2001 From: Luis Cordeiro Date: Sun, 6 Dec 2020 19:16:12 +0000 Subject: [PATCH 3/3] Update UriLocalizer.php --- src/UriLocalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UriLocalizer.php b/src/UriLocalizer.php index c9cc96b..ace5e8b 100644 --- a/src/UriLocalizer.php +++ b/src/UriLocalizer.php @@ -153,6 +153,6 @@ protected function removeTrailingSlash($path) * @return string */ protected function removeBaseUrl($url) { - return str_ireplace(\URL::to('/'), "", $url) ?: '/'; + return str_ireplace(\URL::to('/'), "", rtrim($url, '/')) ?: '/'; } }