PHP code example of tm / silex-sitemap-service-provider

1. Go to this page and download the library: Download tm/silex-sitemap-service-provider 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/ */

    

tm / silex-sitemap-service-provider example snippets


    $app->register(new TM\Provider\SitemapServiceProvider());

    $app->register(new TM\Provider\SitemapServiceProvider(), [
        'sitemap.options' => [
            'charset' => 'ISO-8859-1',
            'version' => '1.0',
            'scheme' => 'http://www.sitemaps.org/schemas/sitemap/0.8'
        ]
    ]);

    $app->get('sitemap.xml', function () use ($app) {
  
      $host = $app['request']->getSchemeAndHttpHost();
      
      $sitemap = $app['sitemap'];
      $sitemap->addEntry($host . '/', 1, 'yearly');
      
      $entities = $app['repository.entity']->findAll(50000);
  
      foreach ($entities as $entity) {
        $entityLoc = $app['url_generator']->generate('entity', array('entity' => $entity->getId()));
        $sitemap->addEntry($host . $entityLoc, 0.8, 'monthly', $entity->getLastModified());
      }
  
      return $sitemap->generate();
    })
    ->bind('sitemap');