PHP code example of fdevs / sitemap

1. Go to this page and download the library: Download fdevs/sitemap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fdevs / sitemap example snippets




namespace FDevs\App\Sitemap\Adapter;

use FDevs\Sitemap\Adapter\AbstractAdapter;

class StaticRouting extends AbstractAdapter
{
    /**
     * @param string $name
     * @param array  $params
     * @param mixed  $item
     *
     * @return Url|null
     */
    public function createUrl($name, array $params = [], $item)
    {

    }

    /**
     * @param array $params
     *
     * @return \Iterator
     */
    public function getItemList(array $params = [])
    {

    }

}


use FDevs\Sitemap\Factory\UrlSet;
use FDevs\Sitemap\Util\Params;

$urlset = new UrlSet();

// your params for the uri
$params = [
    ['_locale' => 'ru', '_format' => 'html'],
    ['_locale' => 'en', '_format' => 'html'],
    ['_locale' => 'ru', '_format' => 'rss'],
    ['_locale' => 'en', '_format' => 'rss'],
];

echo $urlset->xmlString($params);
//<?xml version="1.0" encoding="UTF-8"

use FDevs\Sitemap\Factory\SiteMapIndex;

$index = new SiteMapIndex('http://domain.ltd','/full/path/to/web/dir');

// your params for the uri
$params = [
    ['_locale' => 'ru', '_format' => 'html'],
    ['_locale' => 'en', '_format' => 'html'],
];

echo $index->xmlString($params);
//<?xml version="1.0" encoding="UTF-8"

use FDevs\Sitemap\Util\Params;
$params = Params::prepare(['_locale'=>['ru','en'],'_format'=>['html','rss']])
//output
//$params = [
//    ['_locale' => 'ru', '_format' => 'html'],
//    ['_locale' => 'en', '_format' => 'html'],
//    ['_locale' => 'ru', '_format' => 'rss'],
//    ['_locale' => 'en', '_format' => 'rss'],
//];

use FDevs\Sitemap\SitemapManager;
use FDevs\Sitemap\Factory\SiteMapIndex;
use FDevs\Sitemap\Factory\UrlSet;
use FDevs\Sitemap\Util\Params;

$index = new SiteMapIndex('http://domain.ltd','/full/path/to/web/dir');
$urlset = new UrlSet();
$params = Params::prepare(['_locale'=>['ru','en'],'_format'=>['html','xml']])

$manager = new SitemapManager();
$manager
    ->add($urlset)
    ->add($index);

$manager->get('index')->xmlString($params);
$manager->get('index')->saveFile('/full/path/to/sitemap.xml',$params);

$manager->get('sitemap')->xmlString($params);
$manager->get('sitemap')->saveFile('/full/path/to/sitemapindex.xml',$params);