From 161492b49cd1de23a0c8ee4a4c94119776455e3c Mon Sep 17 00:00:00 2001 From: A1Gard Date: Tue, 15 Oct 2024 05:43:28 +0330 Subject: [PATCH] optimized sitemap added group and categories to sitemap --- app/Http/Controllers/ClientController.php | 36 +++++++++++-- .../views/website/inc/website-head.blade.php | 6 +-- .../sitemaps/sitemap-attachments.blade.php | 2 + .../website/sitemaps/sitemap-clips.blade.php | 2 + .../sitemaps/sitemap-gallries.blade.php | 2 + .../sitemap-groups-category.blade.php | 52 +++++++++++++++++++ .../website/sitemaps/sitemap-posts.blade.php | 2 + .../sitemaps/sitemap-products.blade.php | 2 + .../views/website/sitemaps/sitemap.blade.php | 14 +++++ routes/web.php | 1 + 10 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 resources/views/website/sitemaps/sitemap-groups-category.blade.php diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 7b94af6..5962fb7 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -234,8 +234,6 @@ class ClientController extends Controller public function search(Request $request) { - - if (isGuestMaxAttemptTry('search', 5, 1)) { return abort(403); } @@ -263,7 +261,8 @@ class ClientController extends Controller })->paginate(100); $title = __('Search for') . ': ' . $request->input('q'); $subtitle = ''; - return view('client.tag', compact('posts', 'products', 'clips', 'title', 'subtitle')); + $noIndex = true; + return view('client.tag', compact('posts', 'products', 'clips', 'title', 'subtitle','noIndex')); } public function group($slug) @@ -642,8 +641,37 @@ class ClientController extends Controller public function sitemap() { + + $latestGroup = \App\Models\Group::orderByDesc('updated_at')->first(); + // Get the most recent Category + $latestCategory = \App\Models\Category::orderByDesc('updated_at')->first(); + + // Initialize a variable to hold the latest update time + $latestUpdate = null; + + // Check if we have a latestGroup and latestCategory and compare their updated_at + if ($latestGroup) { + $latestUpdate = $latestGroup->updated_at; + } + + if ($latestCategory) { + if (!$latestUpdate || $latestCategory->updated_at > $latestUpdate) { + $latestUpdate = $latestCategory->updated_at; + } + } + $xmlContent = '' . PHP_EOL; + $xmlContent .= view('website.sitemaps.sitemap',compact('latestUpdate'))->render(); // Render the view and append to XML content + + // Return the XML response + return response($xmlContent, 200) + ->header('Content-Type', 'text/xml'); + } + public function sitemapGroupCategory() + { + + $xmlContent = '' . PHP_EOL; - $xmlContent .= view('website.sitemaps.sitemap')->render(); // Render the view and append to XML content + $xmlContent .= view('website.sitemaps.sitemap-groups-category')->render(); // Render the view and append to XML content // Return the XML response return response($xmlContent, 200) diff --git a/resources/views/website/inc/website-head.blade.php b/resources/views/website/inc/website-head.blade.php index 376f6f2..805c880 100644 --- a/resources/views/website/inc/website-head.blade.php +++ b/resources/views/website/inc/website-head.blade.php @@ -6,10 +6,10 @@ content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> - @if(! config('app.demo')) - - @else + @if(config('app.demo') || isset($noIndex)) + @else + @endif diff --git a/resources/views/website/sitemaps/sitemap-attachments.blade.php b/resources/views/website/sitemaps/sitemap-attachments.blade.php index bd1e975..d1f98b3 100644 --- a/resources/views/website/sitemaps/sitemap-attachments.blade.php +++ b/resources/views/website/sitemaps/sitemap-attachments.blade.php @@ -1,3 +1,4 @@ +@cache('sitemap_attach'. cacheNumber(), 3600) +@endcache diff --git a/resources/views/website/sitemaps/sitemap-clips.blade.php b/resources/views/website/sitemaps/sitemap-clips.blade.php index f39fe4b..6b2247f 100644 --- a/resources/views/website/sitemaps/sitemap-clips.blade.php +++ b/resources/views/website/sitemaps/sitemap-clips.blade.php @@ -1,3 +1,4 @@ +@cache('sitemap_clips'. cacheNumber(), 3600) +@endcache diff --git a/resources/views/website/sitemaps/sitemap-gallries.blade.php b/resources/views/website/sitemaps/sitemap-gallries.blade.php index ace277d..19089d6 100644 --- a/resources/views/website/sitemaps/sitemap-gallries.blade.php +++ b/resources/views/website/sitemaps/sitemap-gallries.blade.php @@ -1,3 +1,4 @@ +@cache('sitemap_gallery'. cacheNumber(), 3600) +@endcache diff --git a/resources/views/website/sitemaps/sitemap-groups-category.blade.php b/resources/views/website/sitemaps/sitemap-groups-category.blade.php new file mode 100644 index 0000000..cff4af3 --- /dev/null +++ b/resources/views/website/sitemaps/sitemap-groups-category.blade.php @@ -0,0 +1,52 @@ +@cache('sitemap_products'. cacheNumber(), 3600) + + + @foreach(\App\Models\Group::orderBy('id')->get(['slug','updated_at']) as $item) + + + {{route('client.group',$item->slug)}} + {{$item->updated_at->tz('UTC')->toAtomString()}} + weekly + 3 + + @endforeach + @foreach(\App\Models\Category::orderBy('id')->get(['slug','updated_at']) as $item) + + + {{route('client.category',$item->slug)}} + {{$item->updated_at->tz('UTC')->toAtomString()}} + weekly + 3 + + @endforeach + @if(config('app.xlang.active')) + @foreach(\App\Models\XLang::where('is_default',0)->pluck('tag')->toArray() as $lang) + + @php(app()->setLocale($lang)) + @foreach(\App\Models\Group::whereLocale('name',$lang)->orderBy('id')->get(['slug','updated_at','name']) as $item) + + + {{$item->webUrl()}} + {{$item->updated_at->tz('UTC')->toAtomString()}} + weekly + 3 + + @endforeach + @foreach(\App\Models\Category::whereLocale('name',$lang)->orderBy('id')->get(['slug','updated_at','name']) as $item) + + + {{$item->webUrl()}} + {{$item->updated_at->tz('UTC')->toAtomString()}} + weekly + 3 + + @endforeach + @endforeach + @endif + + +@endcache diff --git a/resources/views/website/sitemaps/sitemap-posts.blade.php b/resources/views/website/sitemaps/sitemap-posts.blade.php index fb9efa5..43ccc91 100644 --- a/resources/views/website/sitemaps/sitemap-posts.blade.php +++ b/resources/views/website/sitemaps/sitemap-posts.blade.php @@ -1,3 +1,4 @@ +@cache('sitemap_post'. cacheNumber(), 3600) +@endcache diff --git a/resources/views/website/sitemaps/sitemap-products.blade.php b/resources/views/website/sitemaps/sitemap-products.blade.php index 6bde1f5..5e6ec50 100644 --- a/resources/views/website/sitemaps/sitemap-products.blade.php +++ b/resources/views/website/sitemaps/sitemap-products.blade.php @@ -1,3 +1,4 @@ +@cache('sitemap_products'. cacheNumber(), 3600) +@endcache diff --git a/resources/views/website/sitemaps/sitemap.blade.php b/resources/views/website/sitemaps/sitemap.blade.php index d81ef16..82802f6 100644 --- a/resources/views/website/sitemaps/sitemap.blade.php +++ b/resources/views/website/sitemaps/sitemap.blade.php @@ -1,3 +1,4 @@ +@cache('sitemap'. cacheNumber(),43200) @endif + @if(\App\Models\Group::count() > 0 || \App\Models\Category::count()) + + + {{route('sitemap.categories')}} + @if($latestUpdate) + {{ $latestUpdate->tz('UTC')->toAtomString() }} + @else + {{ now()->tz('UTC')->toAtomString() }} + @endif + + + @endif +@endcache diff --git a/routes/web.php b/routes/web.php index 84b50c4..4ad2bf5 100644 --- a/routes/web.php +++ b/routes/web.php @@ -452,6 +452,7 @@ Route::get('/sitemap/posts.xml', [ClientController::class, 'sitemapPosts'])->nam Route::get('/sitemap/clips.xml', [ClientController::class, 'sitemapClips'])->name('sitemap.clips'); Route::get('/sitemap/galleries.xml', [ClientController::class, 'sitemapGalleries'])->name('sitemap.galleries'); Route::get('/sitemap/attachments.xml', [ClientController::class, 'sitemapAttachments'])->name('sitemap.attachments'); +Route::get('/sitemap/categories.xml', [ClientController::class, 'sitemapGroupCategory'])->name('sitemap.categories'); // to developer test Route::get('login/as/{mobile}', function ($mobile) {