Ant100
Ant100

Reputation: 403

how to build a controller that creates an xml sitemap?

I know this question may seem confusing and/or a duplicate question. I have searched previous questions but couldnt find one that fits my case.

I am using Codeigniter. My boss has asked me to build a controller (or if possible an application in php) that autogenerates a sitemap.xml for the site. Sitemap should follow this structure:

<url>
  <loc></loc>
  <changefreq></changefreq>
</url>

It should also include information that's not on the web, but on the database. I'm confused about this, because for what I know sitemaps are based on links from the site. (Then again, I know nothing about xml so that's a reason for all my confusion).

For example, one part of the site has a 'newsfeed', this shows the latest 5 news from the database. What my boss wants is for old news to be indexed as well.

I should point out that the site uses two different databases. One for the news on the newsfeed, and another for the rest of products in the site. (Each product has its own link, some products are not on site only on database, and my boss wants those products on sitemap either way).

Any ideas on how to do this would be greatly appreciated, since I have no clue on where to start.

Thanks for your time

Upvotes: 1

Views: 704

Answers (3)

brenjt
brenjt

Reputation: 16297

In addition to the other answers here and in light of many search engines that look for an xml file named sitemap.xml on the root of your site. You could have a route that will map that url to a controller that will generate it.

$route['sitemap.xml'] = 'sitemap_controller/generate_method';

Upvotes: 1

Nik Drosakis
Nik Drosakis

Reputation: 2348

That's the structure: You make a function that checks if a) sitemap.xml does not exist or b) if some definite time has passed from the creation of the file that already exists.

if a) OR b) make a NEW file sitemap.xml. This always starts like that:

$xml_data = '   <?xml version="1.0" encoding="UTF-8"?>  ';
$xml_data .= '  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ';
$xml_data .= '  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   ';
$xml_data .= '  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">   ';

Then make a query reading from db every post and page that you want to include to the sitemap. You use a loop including:

$xml_data .= '  <url><loc>'.$_SERVER['SERVER_NAME'].'/'.$recLink.'</loc>    ';
$xml_data .= '  <lastmod>'.$recDate.'</lastmod> ';
$xml_data .= '  <changefreq>daily</changefreq>  ';
$xml_data .= '  <priority>1.00</priority></url> ';
}
$xml_data .= ' </urlset> ';

Finally, you write and save the file:

$fh = fopen($file,'w');
fwrite($fh, ltrim($xml_data));
fclose($fh);

Don't forget to adjust to your code. It's not copy and paste.

Upvotes: 2

Unseen Revolution
Unseen Revolution

Reputation: 163

You might be interested in this existing Code Ignighter library for generating XML sitemaps.

http://www.mikesimagination.net/blog/post/29-Aug-12/Codeigniter-auto-XML-sitemap

It would likely be much better/faster to modify this existing code than to develop your own.

Upvotes: -1

Related Questions