Skip to content

Instantly share code, notes, and snippets.

@imanilchaudhari
Created July 14, 2016 10:12
Show Gist options
  • Save imanilchaudhari/13ea436c0ca34bfb053fca24024fcab5 to your computer and use it in GitHub Desktop.
Save imanilchaudhari/13ea436c0ca34bfb053fca24024fcab5 to your computer and use it in GitHub Desktop.
<?php
namespace backend\helpers;
use Yii;
use yii\db\Query;
use yii\base\Object;
use SitemapPHP\Sitemap;
/**
*
* This is based on https://github.com/evert/sitemap-php/
*
**/
class SitemapHelper extends Object
{
protected static function getSlug($string)
{
$hyphened = preg_replace('/\s+/', '-', trim($string, " ")); // Replaces all spaces with hyphens.
$alpha_num = preg_replace('/[^A-Za-z0-9\-]/', '', $hyphened); // stores only alphnumeric characters into $string
$string = preg_replace('/[--]+/', '-', $alpha_num);
return strtolower($string);
}
public static function updateSitemap(){
$sitemap_path = '/path/to/public_html/sitemap.xml';
if (file_exists($sitemap_path)) {
shell_exec('rm -rf '.$sitemap_path);
}
$sitemap = new Sitemap('http://example.com');
$sitemap->setPath('/path/to/public_html/');
$sitemap->setFilename('sitemap');
$query = (new Query)->from('page')->where(['status' => 1]);
foreach ($query->each() as $page) {
$sitemap->addItem('/'.$page['url'], '0.8', 'daily', date('Y-m-d', $page['updated_at']));
}
$sitemap->createSitemapIndex('http://example.com/', 'Today');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment