Luminova Framework

PHP Luminova: Website Sitemap XML Generator

Last updated: 2025-08-24 12:18:34

Generate an XML sitemap for your website using Luminova's novakit command or programmatically.

Luminova’s Sitemap Generator is a built-in system that creates XML sitemaps for your application. You can generate a sitemap in three ways:

  1. Command Line: Use the novakit CLI tool.
  2. Programmatically: Call the sitemap generator from your code.
  3. Background Queue: Run sitemap generation in the background for production environments.

This makes it easy to keep your sitemap up to date, whether during development or in live systems.

Creating a sitemap for your website in Luminova Framework is simple. Just follow these steps:

1. Configure the Sitemap

Open the sitemap configuration file:

/app/Config/Sitemap.php

Update the properties to match your project needs. Common settings include:

  • Start URL – The base URL to begin scanning.
  • Ignore URLs – URLs you don’t want included in the sitemap.
  • Maximum scan depth – Limit how many URLs to scan.
  • Execution time – Maximum time allowed for sitemap generation.

After making changes, run the sitemap command again to apply them.


2. Generate the Sitemap

Navigate to your project directory in the command line interface (CLI) and run the following command to generate your sitemap:

php novakit sitemap

Using Cron in Production

To automatically generate sitemaps every 2 days, configure a cron job like this:

# Run sitemap generation every 2 days at 5:00 AM
0 5 */2 * * flock -n /tmp/task.lock /usr/bin/php /path/to/project/novakit sitemap

Explanation:

  • 0 5 */2 * * → Executes at 05:00 every 2 days.
  • flock → Ensures only one instance runs at a time to prevent overlapping processes.
  • /usr/bin/php /path/to/project/novakit sitemap → Calls the Luminova sitemap generator.

This is the recommended production approach for scheduling sitemaps automatically.

Get Help:

To see all available options and learn more about the sitemap generator:

php novakit sitemap --help

Programmatic Implementation

You can programmatically generate sitemaps in Luminova and schedule them to run periodically using a background task queue or your own console command. Both options are production-ready.


This is the simplest way to run the sitemap generator automatically at fixed intervals.

// app/Tasks/TaskQueue.php
namespace App\Tasks;

use Luminova\Base\BaseTaskQueue;

class TaskQueue extends BaseTaskQueue 
{
    public function tasks(): ?array
    {
        return [
            [
                'handler'   => 'Luminova\Seo\Sitemap::generate', // Method to execute
                'arguments' => [APP_URL],                        // Base URL of your site
                'priority'  => 5,                                // Lower = higher priority
                'forever'   => 2880                              // Run once every 2 days (in minutes)
            ]
        ];
    }
}

Running the task queue manually

php novakit task:run

Automating with cron in production

* * * * * /usr/bin/php /path/to/project/novakit task:run --flock-worker

Use --flock-worker to prevent overlapping executions.


Using a Novakit Console Command

This option use novakit command-line helper to execute commands similar to using BaseCommand but without routing attributes and it doesn't use routing system for execution making it faster in execution process. You can trigger sitemap generation on demand.

// app/Console/SitemapCommand.php
namespace App\Console;

use Luminova\Base\BaseConsole;
use Luminova\Seo\Sitemap;
use Throwable;

class SitemapCommand extends BaseConsole
{
    protected string $name = 'Sitemap';

    // Avoid conflicts with Luminova's built-in sitemap command
    protected string $group = 'app-sitemap'; 

    protected function onCreate(): void
    {
        // Ensures CLI errors are not suppressed
        setenv('throw.cli.exceptions', 'true');
    }

    public function run(?array $options = []): int
    {
        $this->term->parse($options);
        $url = $this->term->getAnyOption('url', 'u', APP_URL);
        $command = trim($this->term->getCommand());

        if ($command !== 'generator') {
            return $this->term->oops($command);
        }

        try {
            if (Sitemap::generate($url, basename: 'sitemap.xml')) {
                $this->term->success('Sitemap was successfully generated.');
                return STATUS_SUCCESS; 
            }
            $this->term->error('Failed to generate sitemap.');
        } catch (Throwable $e) {
            $this->term->error('Error generating sitemap: ' . $e->getMessage());
        }

        return STATUS_ERROR;
    }
}

Register Console command

Register your sitemap command in /bin/.novakit-console.php

// bin/.novakit-console.php
use Luminova\Command\Novakit;
use App\Console\SitemapCommand;

Novakit::command('app-sitemap', SitemapCommand::class);

Run Console command

You can run command manually or use cron in production:

php novakit app-sitemap generator --url=https://example.com

Using Routable Base Command

Using Routable CLI commands via Luminova\Base\BaseCommand. These commands can be executed through public/index.php rather than the novakit helper in the project root.

The example below shows how to generate a sitemap using the built-in Sitemap generator class.

// /app/Controllers/Cli/SitemapCommand.php

namespace App\Controllers\Cli;

use Luminova\Base\BaseCommand;
use Luminova\Seo\Sitemap;
use Luminova\Attributes\Group;
use Luminova\Attributes\Route;
use Throwable;

#[Group('sitemap')]
class SitemapCommand extends BaseCommand
{
    /** @var string $group Group name */
    protected string $group = 'sitemap';

    protected function onCreate(): void
    {
        // Ensure CLI exceptions are thrown
        setenv('throw.cli.exceptions', 'true'); 
    }

    /**
     * Generates the sitemap for the website.
     *
     * @return int Status code: STATUS_SUCCESS or STATUS_ERROR
     */
    #[Route('generator', group: 'sitemap')]
    public function generator(): int 
    {
        try {
            $url = $this->getAnyOption('url', 'u', APP_URL);

            if (Sitemap::generate($url, basename: 'sitemap.xml')) {
                $this->success('Sitemap was successfully generated.');
                return STATUS_SUCCESS; 
            }

            $this->error('Failed to generate sitemap.');
        } catch (Throwable $e) {
            $this->error('Error generating sitemap: ' . $e->getMessage());
        }

        return STATUS_ERROR;
    }
}

Run Routable command

Navigate to your public directory and execute:

php index.php sitemap generator --url=https://example.com

Class Definition

  • Class namespace: \Luminova\Seo\Sitemap

Methods

This method uses the default sitemap configuration by default, but you can provide a custom App\Config\Sitemap object to override settings.

generate

Generate a sitemap and save it in the public directory.

public static generate(
    ?string $url = null, 
    Terminal|LazyInterface|null $term = null, 
    string $basename = 'sitemap.xml',
    ?App\Config\Sitemap<Luminova\Base\BaseConfig> $config = null
): bool

Parameters:

ParameterTypeDescription
$urlnull|stringThe starting URL for sitemap generation (default: null).
$termTerminal|LazyInterface|nullTerminal instance used when generating a sitemap via CLI (default: null)
$basenamestringThe filename to save the sitemap as (e.g, sitemap.xml).
$configSitemap|nullOptional sitemap configuration object. (e.g, sitemap.xml).

Return:

  • bool - Return true if successful, false otherwise.

Throws:

  • Luminova\Exceptions\RuntimeException - If called outside a CLI environment.