]> git.localhorst.tv Git - alttp.git/commitdiff
sitemap xml controller
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 31 Mar 2022 14:45:50 +0000 (16:45 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 31 Mar 2022 14:45:50 +0000 (16:45 +0200)
app/Http/Controllers/SitemapXmlController.php [new file with mode: 0644]
app/Models/SitemapUrl.php [new file with mode: 0644]
resources/views/sitemap.blade.php [new file with mode: 0644]
routes/web.php

diff --git a/app/Http/Controllers/SitemapXmlController.php b/app/Http/Controllers/SitemapXmlController.php
new file mode 100644 (file)
index 0000000..35302c4
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\SitemapUrl;
+use App\Models\Tournament;
+use Illuminate\Http\Request;
+
+class SitemapXmlController extends Controller
+{
+
+       public function index() {
+               $urls = [];
+
+               foreach (Tournament::all() as $tournament) {
+                       $url = new SitemapUrl();
+                       $url->path = '/tournaments'.$tournament->id;
+                       $url->lastmod = $tournament->updated_at ? $tournament->updated_at : ($tournament->created_at ? $tournament->created_at : now());
+                       $url->changefreq = $tournament->locked ? 'never' : 'daily';
+                       $url->priority = $tournament->locked ? 0.5 : 1.0;
+                       $urls[] = $url;
+               }
+
+               return response()->view('sitemap', [
+                       'urls' => $urls,
+               ])->header('Content-Type', 'text/xml');
+       }
+
+}
diff --git a/app/Models/SitemapUrl.php b/app/Models/SitemapUrl.php
new file mode 100644 (file)
index 0000000..0d542a5
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Models;
+
+class SitemapUrl {
+
+       public $path = '';
+       public $lastmod = null;
+       public $changefreq = 'never';
+       public $priority = 0.0;
+
+}
diff --git a/resources/views/sitemap.blade.php b/resources/views/sitemap.blade.php
new file mode 100644 (file)
index 0000000..f9cf5bf
--- /dev/null
@@ -0,0 +1,11 @@
+<?php echo '<?xml version="1.0" encoding="UTF-8"?>', "\n"; ?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+@foreach ($urls as $url)
+       <url>
+               <loc>{{ url($url->path) }}</loc>
+               <lastmod>{{ $url->lastmod->tz('UTC')->toAtomString() }}</lastmod>
+               <changefreq>{{ $url->changefreq }}</changefreq>
+               <priority>{{ $url->priority }}</priority>
+       </url>
+@endforeach
+</urlset>
index 903ca72c0694d6e4dfc3b3499978df73e99c5109..450d21f5ab5b2dcc50ca20cfa5c2dc58dc8ba74a 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 use App\Http\Controllers\DiscordController;
+use App\Http\Controllers\SitemapXmlController;
 use Illuminate\Support\Facades\Route;
 
 /*
@@ -14,12 +15,14 @@ use Illuminate\Support\Facades\Route;
 |
 */
 
+Route::get('/sitemap.xml', [SitemapXmlController::class, 'index']);
+
 Route::view('/{path?}', 'app')->where('path', '.*');
 
 Route::group(['prefix' => config('larascord.prefix'), 'middleware' => ['web']], function() {
-    Route::get('/callback', [DiscordController::class, 'handle'])
-        ->name('larascord.login');
+       Route::get('/callback', [DiscordController::class, 'handle'])
+               ->name('larascord.login');
 
-    Route::redirect('/refresh-token', '/login')
-        ->name('larascord.refresh_token');
+       Route::redirect('/refresh-token', '/login')
+               ->name('larascord.refresh_token');
 });