From 61a76ac8a7edf53052f819d6b578ddebaeb5d3c1 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Mon, 4 Sep 2017 22:01:02 +0100 Subject: [PATCH] #24: Added a basic sitemap.xml with the root gallery URL --- .../Controllers/Gallery/DefaultController.php | 25 +++++++++++++++++++ routes/web.php | 1 + 2 files changed, 26 insertions(+) diff --git a/app/Http/Controllers/Gallery/DefaultController.php b/app/Http/Controllers/Gallery/DefaultController.php index 93de77b..afd7650 100644 --- a/app/Http/Controllers/Gallery/DefaultController.php +++ b/app/Http/Controllers/Gallery/DefaultController.php @@ -34,4 +34,29 @@ class DefaultController extends Controller 'success' => $resetStatus ]); } + + public function sitemapXml() + { + $xml = new \DOMDocument('1.0', 'UTF-8'); + $xml->preserveWhiteSpace = true; + $xml->formatOutput = true; + + $root = $xml->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset'); + $xml->appendChild($root); + + // Root URL + $this->createSitemapNode($xml, $root, route('home')); + + return response($xml->saveXML(), 200, ['Content-Type' => 'text/xml']); + } + + private function createSitemapNode(\DOMDocument $document, \DOMElement $parentElement, $url) + { + $urlElement = $document->createElement('url'); + $urlLocElement = $document->createElement('loc', $url); + $urlElement->appendChild($urlLocElement); + $parentElement->appendChild($urlElement); + + return $urlElement; + } } \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 8a42497..9a138c6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -68,6 +68,7 @@ Route::get('/', 'Gallery\DefaultController@index')->name('home'); Route::get('/activate/{token}', 'Auth\ActivateController@activate')->name('auth.activate'); Route::get('/password/change', 'Auth\ChangePasswordController@showChangePasswordForm')->name('auth.changePassword'); Route::post('/password/change', 'Auth\ChangePasswordController@processChangePassword')->name('auth.processChangePassword'); +Route::get('/sitemap.xml', 'Gallery\DefaultController@sitemapXml'); Route::get('a/{albumUrlAlias}', 'Gallery\AlbumController@index') ->name('viewAlbum') ->where('albumUrlAlias', '.*');