Breaking News
You are here : Home / Web development / CakePHP / XML sitemap generation in cakePHP
CakePHP sitemap Generaion

XML sitemap generation in cakePHP

CakePHP XML Sitemap Generation [starthumbsblock tpl=25]

Howdy Guys ! XML Sitemap generation in cakePHP is very easy.Sitemaps are very important for your webpages to get indexed fast by the Search engines like google , yahoo , ask etc HD59ZQHTDFZ5 .So i go Today a very simple tutorial in XML sitemap Generation in cakePHP.CakePHP has made it quite easy though.This Sitemap will generate you Urls from any model you want.

CakePHP XML Sitemap Generation :: Tutorial-

Now we start our Tutorial to create cakePHP XML sitemap.First we need to know how XML sitemap looks what is there structure,Don’t worry we won’t going to depths we’ll see the basic structure of the XML sitemap.You can see below the Sample XML sitemap Structure.Its Quite Simple.

CakePHP XML sitemap Tutorial – Definition of XML sitemap

Lets Move to our tutorial and see how to create Xml sitemap in cakePHP .

Sample XML Sitemap Structure:

Lets See the tag Definition of this sample XML sitemap.

Sitemap XML tags :

urlset Encapsulates All the Url Set Required
url The Parent tag for each URL entry Required
loc Refers to the Url of the page or post Required
priority Priority of web page relative to other web pages on same website. optional
changefreq Time period in which most likely change on page is expected. optional
lastmod The date when page was last modified. optional

So i hope you understand the XML sitemap Structure .now we move to CakePHP and generate the XML sitemap using cakePHP way.


CakePHP XML Sitemap Generation – Sitemap Controller :-

So Our First Step is to Create sitemap controller which will be used to generate our sitemap.we’ll call our controller sitemaps_controller.php .The controller has one action called  index ,as this is a basic level for understanding.You can add more functionality when you understand this like pinging ,generating robots files.So lets see you sitemap controller code.


The Controller code is very much clear .You can see we want to generate sitemap for our posts and the pages.So we used them here.Whatever model we wana use we can put its name in $uses array and then we can easily include that in out XML sitemap.

Also XML layout in cakePHP are different than HTML , We want to create a empty layout for our XML sitemap.Lets take a look at Layout.

CakePHP XML Sitemap Generation -XML Layout :-

We must create a empty XML layout for sitemap>lets see the layout file.WE’ll name it empty.ctp .

Create Empty xml layout (/app/views/layouts/xml/empty.ctp)

So that how we create a empty layout now we are very near to xml sitemap generation in cakephp.The Next step is to create the view file for the index action we have just created in the sitemapsController.

CakePHP XML Sitemap Generation -View Setup :-

Now we gonna crete a index.ctp file in the /app/views/sitemaps/xml/index.ctp which is going to be the view file for the Action index.Here we gonna loop through every post and page to generate Xml Sitemap in cakePHP.Lets take a look at the view file.

Create the sitemap view (/app/views/sitemaps/xml/index.ctp) :

You can see the code is quite simple we just looped through the two model fields,which i have included in sitemapsController.Now there is just 1 step to Generate CakePHP XML sitemap.

CakePHP XML Sitemap Generation -Router Setup :-

Now we gonna setup out router which is the final step to Generate XML sitemap.WE need to tell router to parse XML Extension and also  we gonna rewirte URLs in router.php so that we can access it in rather than .So lets see out Router code.Add the following code in router.php


That is it Now visit and you will see your XML Sitemap Generated Dynamically in cakePHP . Now You can Submit Your site map to Google webmaster.

I hope you liked this post.Thanks for visting and reading our content .keep visiting for latest tutorials on web development and web design.


Tags : , , , , ,

4 Replies

  1. shikhar subedi says:

    good article..i want some clarification. pages and posts will change frequently . so every time the search engine acceses sitemap.xml , will the changes be reflected in sitemap.xml?

    1. hungnt151 says:

      Thank for your article. I’m a beginner, I have some confuse about sitemap – such as Google SE, I have to upload a sitemap file to Google SE, so Do I always extract and update the sitemap file for Google SE monthly? Is it correctly?

  2. kweiss says:

    I tried this and things seem to work half way because I get this error:

    Not Found/sitemap.xml

    help is highly appreciated.

    1. Make Sure you created the layout file and the view file for the xml sitemap…The LAyout and view files resides on different folder than normal views.

Leave a Reply

Want to join the discussion ? Feel Free to contribute !