Giter VIP home page Giter VIP logo

sitemap-generator's Introduction

Sitemap Generator

Sitemap Generator is a lightweight PHP class that can generate sitemaps.

Installation

$ composer require iulyanp/sitemap-generator

How it works

The XmlSitemapGenerator class can generate Google sitemaps in xml formats.

<?php

use Iulyanp\Sitemap\SitemapConfig;
use Iulyanp\Sitemap\XmlSitemapGenerator;

$config = new SitemapConfig('http://www.google.com', 'http://m.google.com/');
$sitemapGenerator = new XmlSitemapGenerator($config);

// generate some static links
$sitemapGenerator->addItem('/', '1.0', 'daily', 'now');
$sitemapGenerator->addItem('/about', '0.4', 'yearly', 'Jul 08');
$sitemapGenerator->addItem('/contact', '0.7', 'monthly', '11-11-2009');

// create the index sitemap for all generated sitemaps
$sitemapGenerator->createSitemapIndex();

The SitemapGenerator needs an instance of SitemapConfig which takes as it's first argument the webDomain and optionally as the second argument the mobileDomain. When you specify the second argument all the sitemap items will contain also a mobile element.

Also you could specify:

  • the path where the sitemaps will be saved with `$config->setPath('/sitemaps/')
  • the filename of the generated sitemaps $config->setFilename('sitemap')
  • the baseUrl with $config->setBaseUrl('sitemap') eg. http://host.domain/{baseUrl}/{filename}
<?php

require_once './vendor/autoload.php';

use Iulyanp\Sitemap\SitemapConfig;
use Iulyanp\Sitemap\SitemapGeneratorInterface;
use Iulyanp\Sitemap\XmlSitemapGenerator;

class AppSitemap
{
    private $sitemapGenerator;

    public function __construct(SitemapGeneratorInterface $sitemapGenerator)
    {
        $this->sitemapGenerator = $sitemapGenerator;
    }
    
    public function generate()
    {
        // generate some static links
        $this->sitemapGenerator->addItem('/', '1.0', 'daily', 'now');
        $this->sitemapGenerator->addItem('/about', '0.8', 'monthly', 'Jun 25');
        $this->sitemapGenerator->addItem('/contact', '0.6', 'yearly', '14-12-2009');

        // create a DB query and dynamically generate the links
        for ($i = 0; $i <= 100000; $i++) {
            $this->sitemapGenerator->addItem('/products/' . $i . '/');
        }

        // create the index sitemap for all generated sitemaps
        $this->sitemapGenerator->createSitemapIndex();
    }
}

$config = new SitemapConfig('http://www.google.com', 'http://m.google.com/');
$config
    ->setPath('/testings/')
    ->setFilename('test')
    ->setBaseUrl('testing');
$sitemapGenerator = new XmlSitemapGenerator($config);

$app = new AppSitemap($sitemapGenerator);
$app->generate();

You can also add extra tags on each item from your sitemap if you extend XmlSitemapGenerator and overwrite the addOptionalTags method.

sitemap-generator's People

Contributors

iulyanp avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.