Next Chapter: CakePHP 2 Model for RSS

Creating a desktop sitemap with CakePHP

2019-05-13 16:25:36.201097+00

In order to propagate our content to search engines we need sitemaps. We will have two different sitemaps: one for desktop browsers and another one for mobile browsers on smartphone. The link for both sitemaps might be different.

Let's start with the model file:

/usr/local/www/logikfabrik.com/cakephp/app/Model/Sitemap.php
query('SELECT id, name,modified FROM bo_book');
  
   // generate the path to webroot
   $path = ROOT.DS.'app'.DS.'webroot';


   // create the file and open it for writing
   $outfile = new File($path.DS.'sitemap.xml', false, 0644);
   $outfile->open('w');

   // write header
   $outfile->write(''."\n");
   $outfile->write('' . "\n");

   // loop through all books and write it to the file
   foreach($books as $b) {
      $outfile->write(''."\n");
      $outfile->write('http://www.logikfabrik.com/books/' .$b['0']['id'] . '/' . $b['0']['name'] . ''."\n");
      $outfile->write('monthly'."\n");
      $outfile->write('1.0'."\n");
      $outfile->write(''."\n");
   }

   $outfile->write('');
   $outfile->close();

}
}

We would like to call the functionality for sitemap creation from the command line. Therefore we need in CakePHP a Shell. Shells are in the subdirectory .../Console/Command/ and must end with "Shell.php".

vi /usr/local/www/logikfabrik.com/cakephp/app/Console/Command/LShell.php

The content of LShell.php is as follow

Sitemap->crtsitemap(); 
        $this->out(print_r($sm, true));
        $this->log(print_r($sm, true));
    }
}
?>

Use this command to run the shell:

/usr/local/bin/php -f /usr/local/www/logikfabrik.com/cakephp/app/Console/cake.php \
l createsitemapdesktop -app /usr/local/www/logikfabrik.com/cakephp/app

the shell gives this output:

<pre> Welcome to CakePHP v2.6.2 Console --------------------------------------------------------------- App : app Path: /usr/local/www/logikfabrik.com/cakephp/app/ --------------------------------------------------------------- </pre>

Lets check the sitemap.xml file:

cat /usr/local/www/logikfabrik.com/cakephp/app/webroot/sitemap.xml 
<pre> <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <url> <loc>http://www.logikfabrik.com/books/1/Rapid Application Development with Open Source Software</loc> <changefreq>monthly</changefreq> <priority>1.0</priority> </url> </urlset> </pre>

To finish we need to talk to the search robots. We tell them the name of the sitemap file

vi /usr/local/www/logikfabrik.com/cakephp/app/webroot/robots.txt

#ganze Website freigeben
User-agent: *
Disallow:
Disallow: /admin/

# url to the sitemap file:
Sitemap: http://www.logikfabrik.com/sitemap.xml

Lastly we need to check if our sitemap is a valid xml file. Therefore we will use free online services such as sitemapinspector.com.

Tag: cakephp2

Previous Chapter: Building Controller Actions for Comments

Next Chapter: CakePHP 2 Model for RSS