How to create dynamic sitemap.xml in codeigniter

You just need to create below function in your controller. And what data you want to insert in xml file please add as per your requirement, you can save all the data in database and fatch in this function.

If you will call that function by browser, sitemap.xml automatically will generate on root folder if sitemap.xml already created then that function call will overwrite it.

public function xmlwrite()
{
 $xmlString = '<?xml version="1.0" encoding="UTF-8"?>
 <urlset
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
// You can use loop here ( Remove this comment )
 <url>
  <loc>Hello Developers</loc>
  <lastmod>Hello World</lastmod>
  <website>https:pankajbatham.website</website>
 </url>';
// You can use loop here ( Remove this comment )
$xmlString .= '</urlset>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
 if($dom->save($_SERVER["DOCUMENT_ROOT"].'/ayushman/sitemap.xml')){ // please make sure if you are using localhost change ayushman to your project folder name or if your project on web server remover ayushman
 echo "Site Map Created SuccessFully";
}else{
 echo "Site Map Created Failed";
}
}