src/controllers/SitemapController.php
public function actionIndex()
    {

        try {
            // try to register the searchengine visit
            $CrawlerDetect = new CrawlerDetect;

            // Check the user agent of the current 'visitor'
            if($CrawlerDetect->isCrawler()) {
                // insert into table!
                $visit = new SitemapCrawlerVisit();
                $visit->name = $CrawlerDetect->getMatches();
                $visit->save();
            }
        } catch(\Exception $err) {

        }
        Craft::$app->response->format = \yii\web\Response::FORMAT_RAW;
        $headers = Craft::$app->response->headers;
        $headers->add('Content-Type', 'text/xml');

        $dom = new \DOMDocument('1.0','UTF-8');
        $dom->formatOutput = true;

        $urlset = $dom->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
        $urlset->setAttributeNS(
            'http://www.w3.org/2000/xmlns/',
            'xmlns:xhtml',
            'http://www.w3.org/1999/xhtml'
        );
        $dom->appendChild($urlset);

        foreach($this->_createEntrySectionQuery()->all() as $item) {
            $loc = $this->getUrl($item['uri'], $item['siteId']);
            if($loc === null) continue;
            
            $url = $dom->createElement('url');
            $urlset->appendChild($url);
            $url->appendChild($dom->createElement('loc', $loc));
            $url->appendChild($dom->createElement('priority', $item['priority']));
            $url->appendChild($dom->createElement('changefreq', $item['changefreq']));
            $dateUpdated = strtotime($item['dateUpdated']);
            $url->appendChild($dom->createElement('lastmod', date('Y-m-d\TH:i:sP', $dateUpdated)));
            if ($item['alternateLinkCount'] > 1) {
                $alternateLinks = $this->_createAlternateSectionQuery($item['elementId'])->all();
                if (count($alternateLinks) > 0) {
                    foreach ($alternateLinks as $alternateItem) {
                        $alternateLoc = $this->getUrl($alternateItem['uri'], $alternateItem['siteId']);
                        if ($alternateLoc === null) continue;

                        $alternateLink = $dom->createElementNS('http://www.w3.org/1999/xhtml', 'xhtml:link');
                        $alternateLink->setAttribute('rel', 'alternate');
                        $alternateLink->setAttribute('hreflang', strtolower($alternateItem['siteLanguate']));
                        $alternateLink->setAttribute('href', $alternateLoc);
                        $url->appendChild($alternateLink);
                    }
                }
            }
        }

        foreach($this->_createEntryCategoryQuery()->all() as $item) {
            $loc = $this->getUrl($item['uri'], $item['siteId']);
            if($loc === null) continue;

            $url = $dom->createElement('url');
            $urlset->appendChild($url);
            $url->appendChild($dom->createElement('loc', $loc));
            $url->appendChild($dom->createElement('priority', $item['priority']));
            $url->appendChild($dom->createElement('changefreq', $item['changefreq']));
            $dateUpdated = strtotime($item['dateUpdated']);
            $url->appendChild($dom->createElement('lastmod', date('Y-m-d\TH:i:sP', $dateUpdated)));



        }
        return $dom->saveXML();
    }
public function actionIndex()
    {

        try {
            // try to register the searchengine visit
            $CrawlerDetect = new CrawlerDetect;

            // Check the user agent of the current 'visitor'
            if($CrawlerDetect->isCrawler()) {
                // insert into table!
                $visit = new SitemapCrawlerVisit();
                $visit->name = $CrawlerDetect->getMatches();
                $visit->save();
            }
        } catch(\Exception $err) {

        }
        Craft::$app->response->format = \yii\web\Response::FORMAT_RAW;
        $headers = Craft::$app->response->headers;
        $headers->add('Content-Type', 'text/xml');

        $dom = new \DOMDocument('1.0','UTF-8');
        $dom->formatOutput = true;

        $urlset = $dom->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
        $urlset->setAttributeNS(
            'http://www.w3.org/2000/xmlns/',
            'xmlns:xhtml',
            'http://www.w3.org/1999/xhtml'
        );
        $dom->appendChild($urlset);

        foreach($this->_createEntrySectionQuery()->all() as $item) {
            $loc = $this->getUrl($item['uri'], $item['siteId']);
            if($loc === null) continue;
            
            $url = $dom->createElement('url');
            $urlset->appendChild($url);
            $url->appendChild($dom->createElement('loc', $loc));
            $url->appendChild($dom->createElement('priority', $item['priority']));
            $url->appendChild($dom->createElement('changefreq', $item['changefreq']));
            $dateUpdated = strtotime($item['dateUpdated']);
            $url->appendChild($dom->createElement('lastmod', date('Y-m-d\TH:i:sP', $dateUpdated)));
            if ($item['alternateLinkCount'] > 1) {
                $alternateLinks = $this->_createAlternateSectionQuery($item['elementId'])->all();
                if (count($alternateLinks) > 0) {
                    foreach ($alternateLinks as $alternateItem) {
                        $alternateLoc = $this->getUrl($alternateItem['uri'], $alternateItem['siteId']);
                        if ($alternateLoc === null) continue;

                        $alternateLink = $dom->createElementNS('http://www.w3.org/1999/xhtml', 'xhtml:link');
                        $alternateLink->setAttribute('rel', 'alternate');
                        $alternateLink->setAttribute('hreflang', strtolower($alternateItem['siteLanguate']));
                        $alternateLink->setAttribute('href', $alternateLoc);
                        $url->appendChild($alternateLink);
                    }
                }
            }
        }

        foreach($this->_createEntryCategoryQuery()->all() as $item) {
            $loc = $this->getUrl($item['uri'], $item['siteId']);
            if($loc === null) continue;

            $url = $dom->createElement('url');
            $urlset->appendChild($url);
            $url->appendChild($dom->createElement('loc', $loc));
            $url->appendChild($dom->createElement('priority', $item['priority']));
            $url->appendChild($dom->createElement('changefreq', $item['changefreq']));
            $dateUpdated = strtotime($item['dateUpdated']);
            $url->appendChild($dom->createElement('lastmod', date('Y-m-d\TH:i:sP', $dateUpdated)));



        }
        return $dom->saveXML();
    }
Handle a request going to our plugin's index action URL,
e.g.: actions/sitemap/default

@return mixed