Creating sitemap on laravel 4.2 and php the old way

Needed something to generate sitemap.xml using laravel and founded this

From

https://github.com/RoumenDamianoff/laravel-sitemap

add to composer.php

"roumen/sitemap": "dev-master#dd8d820580a48394bae67c738e3f0ec4cc1d8793"

add to app.php

'Roumen\Sitemap\SitemapServiceProvider',

On your route.php

Route::get('sitemap', function(){
// create new sitemap object
$sitemap = App::make("sitemap");
// set cache (key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean))
// by default cache is disabled
//$sitemap->setCache('laravel.sitemap', 3600);

// check if there is cached sitemap and build new only if is not
/*if (!$sitemap->isCached())
{*/
// add item to the sitemap (url, date, priority, freq)
$sitemap->add(URL::to('/'), '2015-03-31T20:10:00+02:00', '1.0', 'daily');
$sitemap->add(URL::to('tentangkami'), '2015-03-31T20:10:00+02:00', '0.9', 'monthly');
$sitemap->add(URL::to('jadwal'), '2015-03-31T20:10:00+02:00', '0.9', 'monthly');
$sitemap->add(URL::to('formulir'), '2015-03-31T20:10:00+02:00', '0.9', 'monthly');
$sitemap->add(URL::to('hubungikami'), '2015-03-31T20:10:00+02:00', '0.9', 'monthly');
//$sitemap->add(URL::to('page'), '2015-03-31T12:30:00+02:00', '0.9', 'monthly');

// get all posts from db
$posts = DB::table('blog')->orderBy('created_at', 'desc')->get();
// add every post to the sitemap
foreach ($posts as $post)
{
$sitemap->add(Url::to('blog/'.$post->id.'/'.urlformat($post->title)), $post->updated_at , '0.9', 'weekly');
}

// get all posts from db
$posts1 = DB::table('gallery')->orderBy('created_at', 'desc')->get();
// add every post to the sitemap
foreach ($posts1 as $post)
{
if (!empty($post->title))
$sitemap->add(Url::to('gallery/'.$post->id.'/'.urlformat($post->title)), $post->updated_at, '0.9', 'weekly');
}
//}

// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
return $sitemap->render('xml');

});

Troubleshoot
There was an error on generating xml

so I went to src/views/xml.blade.php
took out the first line and it worked

{!! '<'.'?'.'xml version="1.0" encoding="UTF-8"?>' !!}

Creating our own sitemap using php mysql and curl

I needed this when I have a list of content that are retrieved by javasript that's why google can only index a bit of my content. So I decided to create my own sitemap.

First create the XML

function sitemap_generate()
{
header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>'."n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

<?php
$sql = "SELECT * FROM `articles`";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
?>
<url>
<loc>http://www.website.com/detail/<?php echo $row['id']; ?>/<?php echo urlformat($row['title']); ?></loc>
<lastmod><?php echo str_replace(' ', 'T', $row['date']).substr(date("O"), 0, -2).':00'; ?></lastmod>
<changefreq>weekly</changefreq>
<priority>0.80</priority>
</url>
<?php } ?>

</urlset>

<?php 
}	

Then Create this page to create sitemap

function sitemap_url()
{
$url = base_url().'scrapper/scrap/sitemap_generate';
$my_file = fopen('sitemap.xml', 'w');

$ch = curl_init($url);
$timeout = 300;

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $my_file);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096);
curl_exec($ch) OR die("Error in curl_exec()");

echo("got to after curl exec");

fclose($my_file);
curl_close($ch);
}

This can be useful, you can use cron with this so you just create a cron hit on function sitemap_url

Subscribe to You Live What You Learn

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe