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"?>
// You can use loop here ( Remove this comment )
  <loc>Hello Developers</loc>
  <lastmod>Hello World</lastmod>
// You can use loop here ( Remove this comment )
$xmlString .= '</urlset>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
 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";
 echo "Site Map Created Failed";