icamys / php-sitemap-generator

A simple PHP sitemap generator.
MIT License
168 stars 65 forks source link

Does not generate a sitemap #21

Closed w4ugit closed 3 years ago

w4ugit commented 3 years ago

An example of my code

$yourSiteUrl = 'https://optklin.com.ua'; $outputDir = getcwd(); $generator = new \Icamys\SitemapGenerator\SitemapGenerator($yourSiteUrl, $outputDir); $generator->toggleGZipFileCreation(); $generator->setMaxURLsPerSitemap(50000); $generator->setSitemapFileName('/frontend/web/sitemap.xml'); $generator->addURL('/', new \DateTime(), 'always', 0.5, []); $generator->createSitemap(); $generator->writeSitemap();

It only creates such a file

`<?xml version="1.0" encoding="UTF-8"?>

https://optklin.com.ua/ 2020-12-08T10:58:14+00:00 always 0.5

`

icamys commented 3 years ago

@w4ugit Looks like you misunderstood a bit the purpose of the variable $outputDir. The $outputDir should store the directory where you expect the sitemap to be generated. And the name of the sitemap without any path should be passed to the method setSitemapFileName() (only sitemap.xml, not /frontend/web/sitemap.xml). So for your case, the following code will work:

include 'src/SitemapGenerator.php';

use Icamys\SitemapGenerator\SitemapGenerator;

$yourSiteUrl = 'https://optklin.com.ua';
$outputDir = getcwd() . '/frontend/web/'; // <-------------- the output directory
$generator = new SitemapGenerator($yourSiteUrl, $outputDir);
$generator->toggleGZipFileCreation();
$generator->setMaxURLsPerSitemap(50000);
$generator->setSitemapFileName('sitemap.xml'); // <--------- the sitemap filename
$generator->addURL('/', new DateTime(), 'always', 0.5, []);
$generator->createSitemap();
$generator->writeSitemap();

Also, ensure that you have appropriate permissions for the directory you want to write the sitemap to.