From 8b333b096d2bacda6adba9c395c65ce283b84c18 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 22 May 2024 20:51:22 +0400 Subject: [PATCH 1/4] add component hreflang tags --- app/Providers/AppServiceProvider.php | 1 + app/View/Components/HreflangTags.php | 41 +++++++++++++++++++ .../views/components/hreflang_tags.blade.php | 3 ++ resources/views/layouts/app.blade.php | 1 + 4 files changed, 46 insertions(+) create mode 100644 app/View/Components/HreflangTags.php create mode 100644 resources/views/components/hreflang_tags.blade.php diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 0511514dc..6ff87b548 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -45,5 +45,6 @@ public function boot(): void Blade::include('components.comments', 'comments'); Blade::include('components.solutions', 'solutions'); Blade::include("components.solution", 'solution'); + Blade::include("components.hreflang_tags", 'hreflang_tags'); } } diff --git a/app/View/Components/HreflangTags.php b/app/View/Components/HreflangTags.php new file mode 100644 index 000000000..dc104dfca --- /dev/null +++ b/app/View/Components/HreflangTags.php @@ -0,0 +1,41 @@ +generateLanguageUrls(); + } + + private function generateLanguageUrls() + { + $defaultLocale = config('app.locale'); + $currentLocale = app()->getLocale(); + $segments = request()->segments(); + + // Combine segments into a URL + $url = implode('/', $segments); + + if ($currentLocale !== $defaultLocale) { + $url = $currentLocale . '/' . $url; + } + + $alternateLocale = ($currentLocale === 'en') ? 'ru' : 'en'; + + $this->languageUrls[$alternateLocale] = $url; + + $this->languageUrls['x-default'] = implode('/', array_slice($segments, 1)); + } + + public function render(): View + { + return view('components.hreflang_tags', ['languageUrls' => $this->languageUrls]); + } +} diff --git a/resources/views/components/hreflang_tags.blade.php b/resources/views/components/hreflang_tags.blade.php new file mode 100644 index 000000000..b43116209 --- /dev/null +++ b/resources/views/components/hreflang_tags.blade.php @@ -0,0 +1,3 @@ +@foreach ($languageUrls as $language => $url) + +@endforeach diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index e5fcd64d8..141927688 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -24,6 +24,7 @@ @includeWhen(app()->environment('production'), 'layouts.deps._gtm_body') @includeWhen(app()->environment('production'), 'layouts.deps._metrika') + From 02e40f8ce84adfc317e733a6018e6bcb4894b557 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 22 May 2024 20:54:40 +0400 Subject: [PATCH 2/4] fix lint --- app/View/Components/HreflangTags.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/View/Components/HreflangTags.php b/app/View/Components/HreflangTags.php index dc104dfca..f0d84f325 100644 --- a/app/View/Components/HreflangTags.php +++ b/app/View/Components/HreflangTags.php @@ -14,7 +14,7 @@ public function __construct() $this->generateLanguageUrls(); } - private function generateLanguageUrls() + private function generateLanguageUrls(): void { $defaultLocale = config('app.locale'); $currentLocale = app()->getLocale(); From 8bb6692830f4024369b6559f11987e40b0157edb Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 23 May 2024 00:32:26 +0400 Subject: [PATCH 3/4] refactor component HreflangTags --- app/View/Components/HreflangTags.php | 23 ++++++++++++------- .../views/components/hreflang_tags.blade.php | 4 +++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/View/Components/HreflangTags.php b/app/View/Components/HreflangTags.php index f0d84f325..96699e51f 100644 --- a/app/View/Components/HreflangTags.php +++ b/app/View/Components/HreflangTags.php @@ -8,6 +8,7 @@ class HreflangTags extends Component { public array $languageUrls = []; + public string $currentLocale; public function __construct() { @@ -17,25 +18,31 @@ public function __construct() private function generateLanguageUrls(): void { $defaultLocale = config('app.locale'); - $currentLocale = app()->getLocale(); + $this->currentLocale = app()->getLocale(); $segments = request()->segments(); - // Combine segments into a URL $url = implode('/', $segments); - if ($currentLocale !== $defaultLocale) { - $url = $currentLocale . '/' . $url; + if ($this->currentLocale !== $defaultLocale) { + $url = "$this->currentLocale/$url"; } - $alternateLocale = ($currentLocale === 'en') ? 'ru' : 'en'; + if ($this->currentLocale === 'ru') { + $this->languageUrls['en'] = $this->removeLanguagePrefixes($url); + } else { + $this->languageUrls['ru'] = "ru/$url"; + } - $this->languageUrls[$alternateLocale] = $url; + $this->languageUrls['x-default'] = $this->removeLanguagePrefixes($url); + } - $this->languageUrls['x-default'] = implode('/', array_slice($segments, 1)); + public function removeLanguagePrefixes(string $url): string + { + return preg_replace('/(^\/ru\/?|\/ru\/?|\/?ru\/?|\/$)/u', '', $url); } public function render(): View { - return view('components.hreflang_tags', ['languageUrls' => $this->languageUrls]); + return view('components.hreflang_tags', ['languageUrls' => $this->languageUrls, 'currentLocale' => $this->currentLocale]); } } diff --git a/resources/views/components/hreflang_tags.blade.php b/resources/views/components/hreflang_tags.blade.php index b43116209..dba740e67 100644 --- a/resources/views/components/hreflang_tags.blade.php +++ b/resources/views/components/hreflang_tags.blade.php @@ -1,3 +1,5 @@ + @foreach ($languageUrls as $language => $url) - + + @endforeach From 97e69f12bccb5ab26af8190c5a16ebf9b1895a65 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 23 May 2024 00:57:43 +0400 Subject: [PATCH 4/4] fix lint --- resources/views/components/hreflang_tags.blade.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/resources/views/components/hreflang_tags.blade.php b/resources/views/components/hreflang_tags.blade.php index dba740e67..8dee5bf28 100644 --- a/resources/views/components/hreflang_tags.blade.php +++ b/resources/views/components/hreflang_tags.blade.php @@ -1,5 +1,3 @@ - @foreach ($languageUrls as $language => $url) - @endforeach