Skip to content

Commit

Permalink
🔍 Add a sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Jan 31, 2024
1 parent 436d8e2 commit 7febfa6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/Http/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Controllers;

use App\Models\Docs;
use Illuminate\Http\Request;

class SitemapController extends Controller
{
/**
* Render the sitemap.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$lastModified = cache()->rememberForever('docs.lastmod', fn () => now()->toAtomString());

$pages = [
'url' => route('home'),
'lastmod' => $lastModified,
];

$pages = [...[$pages], ...Docs::all()->map(fn ($page) => [
'url' => $page->url,
'lastmod' => $lastModified,
])];

return response()
->view('sitemap', ['pages' => $pages])
->header('Content-Type', 'application/xml');
}
}
2 changes: 2 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
User-agent: *
Disallow:

Sitemap: https://laracord.com/sitemap.xml
9 changes: 9 additions & 0 deletions resources/views/sitemap.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach ($pages as $page)
<url>
<loc>{{ $page['url'] }}</loc>
<lastmod>{{ $page['lastmod'] }}</lastmod>
</url>
@endforeach
</urlset>
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@

Route::get('/docs/{page:slug}', DocsShow::class)
->name('docs.show');

Route::get('/sitemap.xml', 'App\Http\Controllers\SitemapController@index');

0 comments on commit 7febfa6

Please sign in to comment.