<?php namespace App\Http\Controllers; use App\Models\Table; use App\Models\User; use XMLWriter; class SitemapController extends Controller { public function sitemap() { ob_end_clean(); // Redirect output to a client’s web browser header("Content-Type: text/xml; charset=utf-8"); // Cache headers header('Cache-Control: max-age=0'); // IE9 header('Cache-Control: max-age=1'); // Other IE headers header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: no-cache'); // HTTP/1.0 $xmlWriter = new XMLWriter(); $xmlWriter->openURI('php://output'); $xmlWriter->startDocument('1.0', 'UTF-8'); $xmlWriter->setIndent(true); { $xmlWriter->startElement('urlset'); { $xmlWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); foreach ($this->iterateTables() as $entry) { $xmlWriter->startElement('url'); $xmlWriter->writeElement('loc', $entry->url); if (isset($entry->priority)) { $xmlWriter->writeElement('priority', $entry->priority); } if (isset($entry->changefreq)) { $xmlWriter->writeElement('changefreq', $entry->changefreq); } if (isset($entry->lastmodified)) { $xmlWriter->writeElement('lastmod', $entry->lastmodified->format(\DateTime::W3C)); } $xmlWriter->endElement(); } } $xmlWriter->endElement(); } $xmlWriter->endDocument(); } private function iterateTables() { $prio_info = .9; $prio_user = .8; $prio_table = .6; $prio_table_rev = .5; $prio_misc = .5; yield (object)[ 'url' => route('dash'), 'priority' => 1, ]; yield (object)[ 'url' => route('terms'), 'priority' => $prio_info, ]; yield (object)[ 'url' => route('faq'), 'priority' => $prio_info, ]; yield (object)[ 'url' => route('donate'), 'priority' => $prio_info, ]; yield (object)[ 'url' => route('privacy'), 'priority' => $prio_info, ]; yield (object)[ 'url' => route('login'), 'priority' => $prio_misc, ]; yield (object)[ 'url' => route('register'), 'priority' => $prio_misc, ]; foreach (User::all() as $user) { yield (object)[ 'url' => route('profile.view', $user->name), 'lastmodified' => $user->updated_at, 'priority' => $prio_user, ]; } foreach (Table::all() as $table) { yield (object)[ 'url' => $table->viewRoute, 'lastmodified' => $table->updated_at, 'priority' => $prio_table, ]; yield (object)[ 'url' => $table->revisionsRoute, 'lastmodified' => $table->updated_at, 'priority' => $prio_table_rev, ]; } } }