SEO: host-aware sitemap.xml + dynamische robots.txt je Domain
Sitemap pro Host (1h-Cache): Hub listet Landing + Rechts-/Info-Seiten, Spokes zusaetzlich Kategorien (lokalisierte Slugs), oeffentliche Newsrooms und alle im Archiv sichtbaren PMs des Portals (explizit ohne PortalScope, forPortalValues). Suche und Marketing-Platzhalterseiten bleiben draussen. Statische public/robots.txt entfernt (haette die Route verschattet); robots.txt jetzt dynamisch mit Sitemap-Verweis und Disallow fuer /admin, /login, /register, Suche. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
a6c3aa6eb8
commit
c2d22c6d65
5 changed files with 251 additions and 2 deletions
126
app/Http/Controllers/Web/SitemapController.php
Normal file
126
app/Http/Controllers/Web/SitemapController.php
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Category;
|
||||
use App\Models\Company;
|
||||
use App\Models\PressRelease;
|
||||
use App\Scopes\PortalScope;
|
||||
use App\Support\WebPortalContext;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* Host-aware XML-Sitemap: Der Hub listet Landing- und Rechts-/Info-Seiten,
|
||||
* die Spokes (presseecho, businessportal24) zusätzlich Kategorien, Newsrooms
|
||||
* und alle im Archiv sichtbaren Pressemitteilungen ihres Portals. Marketing-
|
||||
* Platzhalterseiten (team, presse, karriere, partner) und die Suche bleiben
|
||||
* bewusst draußen (R2.4/R3.2). Pro Host eine Stunde gecacht.
|
||||
*/
|
||||
class SitemapController extends Controller
|
||||
{
|
||||
/** @var list<string> */
|
||||
private const HUB_PAGES = [
|
||||
'faq', 'kontakt', 'ueber-uns', 'hilfe', 'api', 'kuendigung',
|
||||
'impressum', 'datenschutz', 'agb', 'cookies',
|
||||
];
|
||||
|
||||
/** @var list<string> */
|
||||
private const PORTAL_PAGES = [
|
||||
'preise', 'veroeffentlichen', 'newsrooms', 'faq', 'kontakt',
|
||||
'ueber-uns', 'hilfe', 'api', 'impressum', 'datenschutz', 'agb', 'cookies',
|
||||
];
|
||||
|
||||
public function __invoke(Request $request): Response
|
||||
{
|
||||
$host = $request->getHost();
|
||||
$base = rtrim($request->getSchemeAndHttpHost(), '/');
|
||||
$isHub = str_contains($host, 'pressekonto');
|
||||
|
||||
$xml = Cache::remember('web.sitemap.'.$host, 3600, function () use ($base, $isHub): string {
|
||||
$urls = $isHub ? $this->hubUrls($base) : $this->portalUrls($base);
|
||||
|
||||
return view('web.sitemap', ['urls' => $urls])->render();
|
||||
});
|
||||
|
||||
return response($xml, 200, ['Content-Type' => 'application/xml; charset=UTF-8']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{loc: string, lastmod: ?string}>
|
||||
*/
|
||||
private function hubUrls(string $base): array
|
||||
{
|
||||
$urls = [['loc' => $base.'/', 'lastmod' => null]];
|
||||
|
||||
foreach (self::HUB_PAGES as $page) {
|
||||
$urls[] = ['loc' => $base.'/de/'.$page, 'lastmod' => null];
|
||||
}
|
||||
|
||||
return $urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{loc: string, lastmod: ?string}>
|
||||
*/
|
||||
private function portalUrls(string $base): array
|
||||
{
|
||||
$context = WebPortalContext::fromRequest();
|
||||
|
||||
$urls = [['loc' => $base.'/de', 'lastmod' => null]];
|
||||
|
||||
foreach (self::PORTAL_PAGES as $page) {
|
||||
$urls[] = ['loc' => $base.'/de/'.$page, 'lastmod' => null];
|
||||
}
|
||||
|
||||
// Kategorien über die lokalisierten Slugs (CategoryTranslation).
|
||||
$categorySlugs = Category::query()
|
||||
->where('is_active', true)
|
||||
->whereIn('portal', $context->portalValues)
|
||||
->with('translations')
|
||||
->get()
|
||||
->map(fn (Category $category) => $category->translations->firstWhere('locale', 'de')?->slug)
|
||||
->filter()
|
||||
->unique();
|
||||
|
||||
foreach ($categorySlugs as $slug) {
|
||||
$urls[] = ['loc' => $base.'/de/category/'.$slug, 'lastmod' => null];
|
||||
}
|
||||
|
||||
// Öffentliche Newsrooms: aktive Firmen mit mindestens einer im Archiv
|
||||
// sichtbaren PM des Portals (gleiche Regel wie NewsroomsPageData).
|
||||
$companies = Company::query()
|
||||
->where('is_active', true)
|
||||
->whereHas('pressReleases', fn ($query) => $query
|
||||
->withoutGlobalScope(PortalScope::class)
|
||||
->visibleInArchive()
|
||||
->forPortalValues($context->portalValues))
|
||||
->get(['slug', 'updated_at']);
|
||||
|
||||
foreach ($companies as $company) {
|
||||
$urls[] = [
|
||||
'loc' => $base.'/de/newsroom/'.$company->slug,
|
||||
'lastmod' => $company->updated_at?->toAtomString(),
|
||||
];
|
||||
}
|
||||
|
||||
// Alle im Archiv sichtbaren Pressemitteilungen des Portals.
|
||||
$releases = PressRelease::query()
|
||||
->withoutGlobalScope(PortalScope::class)
|
||||
->visibleInArchive()
|
||||
->forPortalValues($context->portalValues)
|
||||
->orderByDesc('published_at')
|
||||
->get(['slug', 'published_at', 'updated_at']);
|
||||
|
||||
foreach ($releases as $release) {
|
||||
$urls[] = [
|
||||
'loc' => $base.'/de/press-release/'.$release->slug,
|
||||
'lastmod' => ($release->updated_at ?? $release->published_at)?->toAtomString(),
|
||||
];
|
||||
}
|
||||
|
||||
return $urls;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
User-agent: *
|
||||
Disallow:
|
||||
11
resources/views/web/sitemap.blade.php
Normal file
11
resources/views/web/sitemap.blade.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{!! '<?xml version="1.0" encoding="UTF-8"?>' !!}
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
@foreach ($urls as $url)
|
||||
<url>
|
||||
<loc>{{ $url['loc'] }}</loc>
|
||||
@if (! empty($url['lastmod']))
|
||||
<lastmod>{{ $url['lastmod'] }}</lastmod>
|
||||
@endif
|
||||
</url>
|
||||
@endforeach
|
||||
</urlset>
|
||||
|
|
@ -9,6 +9,7 @@ use App\Http\Controllers\Web\NewsroomDetailController;
|
|||
use App\Http\Controllers\Web\NewsroomsController;
|
||||
use App\Http\Controllers\Web\PreiseController;
|
||||
use App\Http\Controllers\Web\ReleaseDetailController;
|
||||
use App\Http\Controllers\Web\SitemapController;
|
||||
use App\Http\Controllers\Web\SucheController;
|
||||
use App\Http\Controllers\Web\TerminationController;
|
||||
use App\Http\Controllers\Web\VeroeffentlichenController;
|
||||
|
|
@ -218,6 +219,31 @@ Route::prefix('{edition}')->whereIn('edition', SetEdition::EDITIONS)->group(func
|
|||
Route::view('cookies', 'web.cookies')->name('cookies');
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SEO: Sitemap + robots.txt, host-aware (Hub vs. Spokes). Die frühere
|
||||
// statische public/robots.txt wurde entfernt — sie hätte diese Routen
|
||||
// verschattet (Webserver liefert existierende Dateien direkt aus).
|
||||
// ---------------------------------------------------------------------------
|
||||
Route::get('/sitemap.xml', SitemapController::class)->name('sitemap');
|
||||
|
||||
Route::get('/robots.txt', function () {
|
||||
$base = rtrim(request()->getSchemeAndHttpHost(), '/');
|
||||
|
||||
$lines = [
|
||||
'User-agent: *',
|
||||
'Disallow: /admin',
|
||||
'Disallow: /login',
|
||||
'Disallow: /register',
|
||||
'Disallow: /de/suche',
|
||||
'Disallow: /en/suche',
|
||||
'',
|
||||
'Sitemap: '.$base.'/sitemap.xml',
|
||||
'',
|
||||
];
|
||||
|
||||
return response(implode("\n", $lines), 200, ['Content-Type' => 'text/plain; charset=UTF-8']);
|
||||
})->name('robots');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sprach-/Edition-unabhängige Routen (API-Doku).
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
|
|||
88
tests/Feature/Web/SitemapRobotsTest.php
Normal file
88
tests/Feature/Web/SitemapRobotsTest.php
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
use App\Enums\Portal;
|
||||
use App\Models\Category;
|
||||
use App\Models\Company;
|
||||
use App\Models\PressRelease;
|
||||
use Tests\TestCase;
|
||||
|
||||
test('robots.txt is served per host with the matching sitemap reference', function () {
|
||||
/** @var TestCase $this */
|
||||
$this->get('https://presseecho.test/robots.txt')
|
||||
->assertSuccessful()
|
||||
->assertHeader('Content-Type', 'text/plain; charset=UTF-8')
|
||||
->assertSee('Disallow: /admin', false)
|
||||
->assertSee('Sitemap: https://presseecho.test/sitemap.xml', false);
|
||||
|
||||
$this->get('https://pressekonto.test/robots.txt')
|
||||
->assertSuccessful()
|
||||
->assertSee('Sitemap: https://pressekonto.test/sitemap.xml', false);
|
||||
});
|
||||
|
||||
test('the spoke sitemap lists static pages, categories, newsrooms and archive releases of its portal', function () {
|
||||
/** @var TestCase $this */
|
||||
$category = Category::factory()->create(['portal' => Portal::Presseecho, 'is_active' => true]);
|
||||
$category->translations()->create(['locale' => 'de', 'name' => 'Technologie', 'slug' => 'technologie']);
|
||||
|
||||
$company = Company::factory()->presseecho()->create(['is_active' => true]);
|
||||
|
||||
PressRelease::factory()
|
||||
->published()
|
||||
->forPortal(Portal::Presseecho)
|
||||
->for($category)
|
||||
->for($company)
|
||||
->create(['slug' => 'ki-report', 'published_at' => now()->subDay()]);
|
||||
|
||||
// Fremd-Portal-PM darf nicht in der presseecho-Sitemap auftauchen.
|
||||
$bpCategory = Category::factory()->create(['portal' => Portal::Businessportal24]);
|
||||
PressRelease::factory()
|
||||
->published()
|
||||
->forPortal(Portal::Businessportal24)
|
||||
->for($bpCategory)
|
||||
->for(Company::factory()->businessportal24()->create())
|
||||
->create(['slug' => 'bp24-only', 'published_at' => now()->subDay()]);
|
||||
|
||||
$response = $this->get('https://presseecho.test/sitemap.xml')->assertSuccessful();
|
||||
|
||||
expect($response->headers->get('Content-Type'))->toContain('application/xml');
|
||||
|
||||
expect($response->getContent())
|
||||
->toContain('<loc>https://presseecho.test/de</loc>')
|
||||
->toContain('<loc>https://presseecho.test/de/preise</loc>')
|
||||
->toContain('<loc>https://presseecho.test/de/impressum</loc>')
|
||||
->toContain('<loc>https://presseecho.test/de/category/technologie</loc>')
|
||||
->toContain('<loc>https://presseecho.test/de/newsroom/'.$company->slug.'</loc>')
|
||||
->toContain('<loc>https://presseecho.test/de/press-release/ki-report</loc>')
|
||||
->not->toContain('bp24-only')
|
||||
// Suche und Marketing-Platzhalterseiten bleiben draußen.
|
||||
->not->toContain('/de/suche')
|
||||
->not->toContain('/de/team');
|
||||
});
|
||||
|
||||
test('the hub sitemap lists the landing and legal pages but no archive content', function () {
|
||||
/** @var TestCase $this */
|
||||
PressRelease::factory()
|
||||
->published()
|
||||
->forPortal(Portal::Presseecho)
|
||||
->for(Category::factory()->create(['portal' => Portal::Presseecho]))
|
||||
->for(Company::factory()->presseecho()->create())
|
||||
->create(['slug' => 'nicht-im-hub', 'published_at' => now()->subDay()]);
|
||||
|
||||
$response = $this->get('https://pressekonto.test/sitemap.xml')->assertSuccessful();
|
||||
|
||||
expect($response->getContent())
|
||||
->toContain('<loc>https://pressekonto.test/</loc>')
|
||||
->toContain('<loc>https://pressekonto.test/de/impressum</loc>')
|
||||
->toContain('<loc>https://pressekonto.test/de/kuendigung</loc>')
|
||||
->not->toContain('press-release')
|
||||
->not->toContain('newsroom');
|
||||
});
|
||||
|
||||
test('marketing pages carry brand-aware titles instead of the hardcoded legacy brand', function () {
|
||||
/** @var TestCase $this */
|
||||
$response = $this->get('https://presseecho.test/de/team')->assertSuccessful();
|
||||
|
||||
expect($response->getContent())
|
||||
->toContain('<title>Unser Team – presseecho</title>')
|
||||
->not->toContain('Business Portal 24');
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue