CakePHP 3 - Best Practice and Cookbook

Next: Building an RSS Feed with CakePHP 3 UP: TOC

Creating a Sitemap for search engines is a useful example to create a cronjob.

Here is the content of the file "...src/Model/Table/SitemapsTable.php":

 'Teilen',
        'fr' => 'partager',
        'en' => 'Sharing',
        'zh' => '分享',
  ];

  public function initialize(array $config)
  {
  }

  public function create() {
        $this->autoRender = false;

        // write files (Desktop: stationäres Internet)
        $path = ROOT.DS.'webroot';
  
        $outfile = new File($path.DS.'sitemap.xml', true, 0644);
        $outfile->open('w');

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

        // Write individuals
        foreach ($this->sm as $url => $txt) {
            $outfile->write(''."\n");
            $outfile->write('https://www.41share.com/' . $url. ''."\n");
            $outfile->write('weekly'."\n");
            $outfile->write('1.0'."\n");
            $outfile->write(''."\n");
        }

        $lng = ['de' => 'Deutsch','fr' => 'Français', 'en' => 'English', 'zh' => 'Chinese'];

        $q = "select id from category";
        $conn = ConnectionManager::get('default');
        $rset = $conn->execute($q)->fetchAll('assoc');
        foreach ($rset as $rec) {
           foreach ($lng as $langcode => $language) {
             $outfile->write(''."\n".'https://www.41share.com/' . $langcode . '/objects/category/' . $rec['id'] . ''."\n");
             $outfile->write('weekly'."\n".'1.0'."\n".''."\n");
           }
        }

        $q = "select id,lang from objects where pubshare = 'y'";
        $conn = ConnectionManager::get('default');
        $rset = $conn->execute($q)->fetchAll('assoc');
        foreach ($rset as $rec) {
           $outfile->write(''."\n".'https://www.41share.com/' . $rec['lang'] . '/objects/shared/' . $rec['id'] . ''."\n");
           $outfile->write('weekly'."\n".'1.0'."\n".''."\n");
        }

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

    public function createmob() {
        $this->autoRender = false;
        $z = 0;
       
        // write files (Mobile Internet)
        $path = ROOT.DS.'webroot';
  
        $outfile = new File($path.DS.'sitemap-mobile.xml', true, 0644);
        $outfile->open('w');

        $outfile->write(''."\n");
        $outfile->write('write('   xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">'. "\n");

        // Write individuals
        foreach ($this->sm as $url => $txt) {
            $outfile->write(''."\n");
            $outfile->write('https://www.41share.com/' . $url. ''."\n");
            $outfile->write('weekly'."\n");
            $outfile->write('1.0'."\n");
            $outfile->write(''."\n");
            $outfile->write(''."\n");
            $z++;
        }

        $q = "select id,lang from objects where pubshare = 'y'";
        $conn = ConnectionManager::get('default');
        $rset = $conn->execute($q)->fetchAll('assoc');
        foreach ($rset as $rec) {
           $outfile->write(''."\n".'https://www.41share.com/'.$rec['lang'].'/objects/shared/'.$rec['id']. ''."\n");
           $outfile->write('weekly'."\n".'1.0'."\n");
           $outfile->write(''."\n");
           $outfile->write(''."\n");
           $z++;
        }

        $q = "select id from categories";
        $rset = $conn->execute($q)->fetchAll('assoc');
        foreach ($rset as $rec) {
           $outfile->write(''."\n".'https://www.41share.com/de/objects/category/' . $rec['id'] . ''."\n");
           $outfile->write('weekly'."\n".'1.0'."\n");
           $outfile->write(''."\n");
           $outfile->write(''."\n");
           $z++;
        }

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

}
?>

loadModel('Sitemaps');
    $rc = $this->Sitemaps->create();
    $rc = $this->Sitemaps->createmob();
  }
}

To start the job in the above file we need a Shell.

This is the content of the file ../src/Shell/ShareShell.php

Next: Building an RSS Feed with CakePHP 3 UP: TOC