Skip to content

Commit

Permalink
Sitemap generator
Browse files Browse the repository at this point in the history
  • Loading branch information
cholladay0816 committed Sep 7, 2023
1 parent db0c362 commit 22a846f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
7 changes: 2 additions & 5 deletions app/Http/Controllers/ExploreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ class ExploreController extends Controller
{
public function index()
{
$users = User::with('creator')
->whereHas('creator', function ($creator) {
$creator->where('open', 1)
->whereHas('commissionPresets');
})->paginate(16);
$users = User::getExploreCreatorQuery()
->paginate(16);

return view(
'explore.index',
Expand Down
73 changes: 73 additions & 0 deletions app/Http/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\View;

class SitemapController extends Controller
{
public function index()
{
$urls = Cache::remember('sitemap', now()->addDays(7), function() {
$creators = User::getExploreCreatorQuery()->get()->map(function($user) {
return [
'path' => route('creator.show', $user),
'lastmod' => now()->firstOfMonth(),
'changefreq' => 'weekly',
'priority' => $user?->rating ?? '0.5'
];
});
return collect([
[
'path' => url('/'),
'lastmod' => now()->firstOfYear(),
'changefreq' => 'yearly',
'priority' => '1'
],
[
'path' => url('thank-you'),
'lastmod' => now()->firstOfYear(),
'changefreq' => 'yearly',
'priority' => '0.9'
],
[
'path' => route('explore'),
'lastmod' => now()->firstOfMonth(),
'changefreq' => 'weekly',
'priority' => '0.9'
],
[
'path' => route('login'),
'lastmod' => now()->firstOfYear(),
'changefreq' => 'yearly',
'priority' => '0.9'
],
[
'path' => route('register'),
'lastmod' => now()->firstOfYear(),
'changefreq' => 'yearly',
'priority' => '0.9'
],
[
'path' => route('terms.show'),
'lastmod' => now()->firstOfMonth(),
'changefreq' => 'monthly',
'priority' => '0.9'
],
[
'path' => url('privacy-policy'),
'lastmod' => now()->firstOfMonth(),
'changefreq' => 'monthly',
'priority' => '0.9'
]
])->concat($creators);
});
$content = View::make('sitemap.index', ['urls' => $urls]);
$response = Response::make($content);
return $response->withHeaders(['Content-Type' => 'application/xml']);
}
}
9 changes: 9 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ public function getRouteKeyName()
return 'name';
}

public static function getExploreCreatorQuery(): \Illuminate\Database\Eloquent\Builder
{
return User::with('creator')
->whereHas('creator', function ($creator) {
$creator->where('open', 1)
->whereHas('commissionPresets');
});
}

public function commissionPresets()
{
return $this->hasMany(CommissionPreset::class);
Expand Down
17 changes: 17 additions & 0 deletions resources/views/sitemap/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach($urls as $url)
<url>

<loc>{{ $url['path'] }}</loc>

<lastmod>{{ $url['lastmod'] ?? now() }}</lastmod>

<changefreq>{{ $url['changefreq'] ?? 'weekly' }}</changefreq>

<priority>{{ $url['priority'] ?? '0.8' }}</priority>

</url>
@endforeach
</urlset>
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
return view('welcome');
})->name('welcome');

Route::get('/sitemap.xml', [\App\Http\Controllers\SitemapController::class, 'index'])
->name('sitemap');

Route::get('thank-you', function () {
return view('thanks');
Expand Down

0 comments on commit 22a846f

Please sign in to comment.