Created
June 20, 2017 12:16
-
-
Save harini-ua/623082527f8e0efb80134b0bc83048fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use App\Post; | |
use Carbon\Carbon; | |
use Illuminate\Support\Facades\Cache; | |
class SiteMap | |
{ | |
/** | |
* Return the content of the Site Map | |
*/ | |
public function getSiteMap() | |
{ | |
if (Cache::has('site-map')) { | |
return Cache::get('site-map'); | |
} | |
$siteMap = $this->buildSiteMap(); | |
Cache::add('site-map', $siteMap, 120); | |
return $siteMap; | |
} | |
/** | |
* Build the Site Map | |
*/ | |
protected function buildSiteMap() | |
{ | |
$postsInfo = $this->getPostsInfo(); | |
$dates = array_values($postsInfo); | |
sort($dates); | |
$lastmod = last($dates); | |
$url = trim(url(), '/') . '/'; | |
$xml = []; | |
$xml[] = '<?xml version="1.0" encoding="UTF-8"?'.'>'; | |
$xml[] = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; | |
$xml[] = ' <url>'; | |
$xml[] = " <loc>$url</loc>"; | |
$xml[] = " <lastmod>$lastmod</lastmod>"; | |
$xml[] = ' <changefreq>daily</changefreq>'; | |
$xml[] = ' <priority>0.8</priority>'; | |
$xml[] = ' </url>'; | |
foreach ($postsInfo as $slug => $lastmod) { | |
$xml[] = ' <url>'; | |
$xml[] = " <loc>{$url}blog/$slug</loc>"; | |
$xml[] = " <lastmod>$lastmod</lastmod>"; | |
$xml[] = " </url>"; | |
} | |
$xml[] = '</urlset>'; | |
return join("\n", $xml); | |
} | |
/** | |
* Return all the posts as $url => $date | |
*/ | |
protected function getPostsInfo() | |
{ | |
return Post::where('published_at', '<=', Carbon::now()) | |
->where('is_draft', 0) | |
->orderBy('published_at', 'desc') | |
->lists('updated_at', 'slug') | |
->all(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment