From 060ff60b318dc85297a8f0c76b92870ac2c20e4f Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Tue, 18 Aug 2020 12:17:17 +0200 Subject: [PATCH 1/7] #735 solves wrong routes matching --- .../LaravelLocalization.php | 129 ++++++++++-------- 1 file changed, 70 insertions(+), 59 deletions(-) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index e4000b5..354bbc4 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -4,6 +4,7 @@ use Illuminate\Config\Repository; use Illuminate\Contracts\Routing\UrlRoutable; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; use Mcamara\LaravelLocalization\Exceptions\SupportedLocalesNotDefined; use Mcamara\LaravelLocalization\Exceptions\UnsupportedLocaleException; @@ -884,83 +885,93 @@ public function setSerializedTranslatedRoutes($serializedRoutes) */ protected function extractAttributes($url = false, $locale = '') { + if (!empty($url)) { - $attributes = []; - $parse = parse_url($url); - if (isset($parse['path'])) { - $parse['path'] = trim(str_replace('/'.$this->currentLocale.'/', '', $parse['path']), "/"); - $url = explode('/', trim($parse['path'], '/')); - } else { - $url = []; - } - - foreach ($this->router->getRoutes() as $route) { + $cacheKey = 'laravellocalisation-ea-'.$locale.'-'.$url; + return Cache::remember($cacheKey, 30, function() use ($url, $locale) { $attributes = []; - $path = method_exists($route, 'uri') ? $route->uri() : $route->getUri(); - - if (!preg_match("/{[\w]+\??}/", $path)) { - continue; + $originalUrl = $url; + $parse = parse_url($url); + if (isset($parse['path'])) { + $parse['path'] = trim(str_replace('/'.$this->currentLocale.'/', '', $parse['path']), "/"); + $url = explode('/', trim($parse['path'], '/')); + } else { + $url = []; } - - $path = explode('/', $path); - $i = 0; - - // The system's route can't be smaller - // only the $url can be missing segments (optional parameters) - // We can assume it's the wrong route - if (count($path) < count($url)) { - continue; - } - - $match = true; - foreach ($path as $j => $segment) { - if (isset($url[$i])) { - if ($segment === $url[$i]) { - $i++; - continue; - } elseif (preg_match("/{[\w]+}/", $segment)) { - // must-have parameters - $attribute_name = preg_replace(['/}/', '/{/', "/\?/"], '', $segment); - $attributes[$attribute_name] = $url[$i]; - $i++; - continue; - } elseif (preg_match("/{[\w]+\?}/", $segment)) { - // optional parameters - if (!isset($path[$j + 1]) || $path[$j + 1] !== $url[$i]) { - // optional parameter taken + + foreach ($this->router->getRoutes() as $route) { + $attributes = []; + $path = method_exists($route, 'uri') ? $route->uri() : $route->getUri(); + + + + if (!preg_match("/{[\w]+\??}/", $path)) { + continue; + } + $path = explode('/', $path); + $i = 0; + + // The system's route can't be smaller + // only the $url can be missing segments (optional parameters) + // We can assume it's the wrong route + if (count($path) < count($url)) { + continue; + } + + + $match = true; + foreach ($path as $j => $segment) { + if (isset($url[$i])) { + if ($segment === $url[$i]) { + $i++; + continue; + } elseif (preg_match("/{[\w]+}/", $segment)) { + // must-have parameters $attribute_name = preg_replace(['/}/', '/{/', "/\?/"], '', $segment); $attributes[$attribute_name] = $url[$i]; $i++; continue; + } elseif (preg_match("/{[\w]+\?}/", $segment)) { + // optional parameters + if (!isset($path[$j + 1]) || $path[$j + 1] !== $url[$i]) { + // optional parameter taken + $attribute_name = preg_replace(['/}/', '/{/', "/\?/"], '', $segment); + $attributes[$attribute_name] = $url[$i]; + $i++; + continue; + } else { + $match = false; + break; + } } else { + // As soon as one segment doesn't match, then we have the wrong route $match = false; break; } + } elseif (preg_match("/{[\w]+\?}/", $segment)) { + $attribute_name = preg_replace(['/}/', '/{/', "/\?/"], '', $segment); + $attributes[$attribute_name] = null; + $i++; } else { - // As soon as one segment doesn't match, then we have the wrong route + // no optional parameters but no more $url given + // this route does not match the url $match = false; break; } - } elseif (preg_match("/{[\w]+\?}/", $segment)) { - $attribute_name = preg_replace(['/}/', '/{/', "/\?/"], '', $segment); - $attributes[$attribute_name] = null; - $i++; - } else { - // no optional parameters but no more $url given - // this route does not match the url + } + + if (isset($url[$i + 1])) { $match = false; - break; + } + + if ($match) { + if($route->matches(request()->create($originalUrl))) { + return $attributes; + } } } - - if (isset($url[$i + 1])) { - $match = false; - } - - if ($match) { - return $attributes; - } - } + return []; + }); } else { if (!$this->router->current()) { return []; From a858ba3aac142b12a2e8d3976231c8609e8e19f0 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Thu, 14 Jan 2021 19:49:44 +0100 Subject: [PATCH 2/7] update package name --- composer.json | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 45a2352..eddb924 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,14 @@ { - "name": "mcamara/laravel-localization", + "name": "nuranto/laravel-localization", "description": "Easy localization for Laravel", "keywords": ["localization", "laravel", "php"], "homepage": "https://github.com/mcamara/laravel-localization", "license": "MIT", - "authors": [ - { - "name": "Marc Cámara", - "email": "mcamara88@gmail.com", - "role": "Developer" - } - ], + "authors": [{ + "name": "Marc Cámara", + "email": "mcamara88@gmail.com", + "role": "Developer" + }], "require": { "php": ">=7.1.0", "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0" @@ -26,8 +24,7 @@ "test": "vendor/bin/phpunit" }, "autoload": { - "classmap": [ - ], + "classmap": [], "psr-0": { "Mcamara\\LaravelLocalization": "src/" } @@ -44,4 +41,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} +} \ No newline at end of file From abb0405aa1e357486e7d5636906a4d932e0c3f45 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Fri, 14 Jan 2022 15:31:50 +0100 Subject: [PATCH 3/7] add parameter to setLocal method --- src/Mcamara/LaravelLocalization/LaravelLocalization.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 3b7f27c..2ebf970 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -151,11 +151,15 @@ public function __construct() * Set and return current locale. * * @param string $locale Locale to set the App to (optional) + * @param bool $asDefault set as default locale * * @return string Returns locale (if route has any) or null (if route does not have a locale) */ - public function setLocale($locale = null) + public function setLocale($locale = null, $asDefault = false) { + if($asDefault) { + $this->defaultLocale = $locale; + } if (empty($locale) || !\is_string($locale)) { // If the locale has not been passed through the function // it tries to get it from the first segment of the url From 86556944268ee4754ccb1361807f642b6a9e65d5 Mon Sep 17 00:00:00 2001 From: baltun Date: Sun, 24 Oct 2021 14:47:26 +0300 Subject: [PATCH 4/7] #760 - exclude http methods from processing to solve 'POST not working' issue (#762) Co-authored-by: Ilya Kolesnikov (cherry picked from commit c71bb119752a28b9d73fb3c1c342594b761bbc93) --- README.md | 4 ++++ .../Middleware/LaravelLocalizationMiddlewareBase.php | 3 +++ src/config/config.php | 1 + 3 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 0f27c0a..f17fd82 100644 --- a/README.md +++ b/README.md @@ -501,6 +501,10 @@ will not work. Instead, one has to use ``` + +Another way to solve this is to put http method to config to 'laravellocalization.httpMethodsIgnored' +to prevent of processing this type of requests + ### MethodNotAllowedHttpException If you do not localize your post url and use a redirect middleware, diff --git a/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php b/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php index 2f2a10e..e12f908 100644 --- a/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php +++ b/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationMiddlewareBase.php @@ -20,6 +20,9 @@ class LaravelLocalizationMiddlewareBase */ protected function shouldIgnore($request) { + if (in_array($request->method(), config('laravellocalization.httpMethodsIgnored'))) { + return true; + } $this->except = $this->except ?? config('laravellocalization.urlsIgnored', []); foreach ($this->except as $except) { if ($except !== '/') { diff --git a/src/config/config.php b/src/config/config.php index c04a302..32d382f 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -344,4 +344,5 @@ // Defaults to [] 'urlsIgnored' => ['/skipped'], + 'httpMethodsIgnored' => ['POST', 'PUT', 'PATCH', 'DELETE'], ]; From 6da50407848b8412b507619bd45c8a0ff5498ddf Mon Sep 17 00:00:00 2001 From: DJEMMAL-nour-el-islam <52753848+djemmal-nour-el-islam@users.noreply.github.com> Date: Sun, 24 Oct 2021 12:55:35 +0100 Subject: [PATCH 5/7] fix bug in switching arabic to another language (#722) to handle the case of switching from arabic to another lang Co-authored-by: DJEMMAL-nour-el-islam <52753848+djemml-nour-el-islam@users.noreply.github.com> (cherry picked from commit 645819da9ef29f3ba7588d9b4598799caf0b2463) --- src/Mcamara/LaravelLocalization/LaravelLocalization.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mcamara/LaravelLocalization/LaravelLocalization.php b/src/Mcamara/LaravelLocalization/LaravelLocalization.php index 2ebf970..38c8e6c 100644 --- a/src/Mcamara/LaravelLocalization/LaravelLocalization.php +++ b/src/Mcamara/LaravelLocalization/LaravelLocalization.php @@ -766,7 +766,7 @@ protected function findTranslatedRouteByUrl($url, $attributes, $locale) $routeName = $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes); // We can ignore extra url parts and compare only their url_path (ignore arguments that are not attributes) - if (parse_url($this->getNonLocalizedURL($routeName), PHP_URL_PATH) == parse_url($this->getNonLocalizedURL($url), PHP_URL_PATH)) { + if (parse_url($this->getNonLocalizedURL($routeName), PHP_URL_PATH) == parse_url($this->getNonLocalizedURL(urldecode($url)), PHP_URL_PATH)) { $this->cachedTranslatedRoutesByUrl[$locale][$url] = $translatedRoute; return $translatedRoute; From 8221d31e96ba36f5dbfda18546d0c8dc17b29620 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Sat, 24 Feb 2024 13:27:45 +0100 Subject: [PATCH 6/7] fix --- composer.json | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index eddb924..efd6255 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,23 @@ { "name": "nuranto/laravel-localization", "description": "Easy localization for Laravel", - "keywords": ["localization", "laravel", "php"], + "keywords": [ + "localization", + "laravel", + "php" + ], "homepage": "https://github.com/mcamara/laravel-localization", "license": "MIT", - "authors": [{ - "name": "Marc Cámara", - "email": "mcamara88@gmail.com", - "role": "Developer" - }], + "authors": [ + { + "name": "Marc Cámara", + "email": "mcamara88@gmail.com", + "role": "Developer" + } + ], "require": { "php": ">=7.1.0", - "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0" + "laravel/framework": "^8.0||^9.0" }, "require-dev": { "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0", From d4d9a7b02efc997b165109008113982b4435ba86 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Sat, 8 Feb 2025 14:34:33 +0100 Subject: [PATCH 7/7] laravel compatibility --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index efd6255..65507de 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": ">=7.1.0", - "laravel/framework": "^8.0||^9.0" + "laravel/framework": "^10.0||^11.0" }, "require-dev": { "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0",