PHP Luminova: Generating an XML Sitemap
Crawl your application and generate an XML sitemap from Novakit, a PHP CLI command, or a background task.
An XML sitemap lists the pages you want search engines to discover. Luminova starts from your configured URL, follows eligible links, and writes the sitemap to the application’s public/ directory. To check which links failed during a crawl, use the Links Audit guide.
Class Definition
- Full namespace:
Luminova\Components\Seo\Sitemap - This class is final.
Generate a sitemap with Novakit
Supported Novakit CLI Options
-u, --url # Specify the starting URL to scan (default: value of `app.start.url`).
-f, --basename # Set the XML output filename (default: sitemap.xml).
-l, --limit # Limit the number of URLs to scan (0 = no limit).
-d, --delay # Delay in seconds between scans (minimum: 1).
-e, --max-execution # Maximum script execution time in seconds (0 = unlimited).
-p, --prefix # Restrict scan to URLs matching a specific prefix.
-c, --change # Set the expected change frequency (always, daily, weekly, etc.).
-s, --html # Include static `.html` versions of URLs in the sitemap.
-n, --dry-run # Perform a full scan without writing any output files.
-h, --help # Show help information for this command.Get Help:
To see all available options and learn more about the sitemap generator:
php novakit sitemap --helpConfigure the crawl
Set crawl defaults in app/Config/Sitemap.php. See the sitemap configuration guide for all available settings.
- Start URL - Set
app.start.urlto the crawl base. Even when you pass--url, that URL must match the configured base and any scan prefix. - Ignore URLs - URLs you don't want included in the sitemap.
- URL limit - Set the maximum number of URLs to scan.
- Execution time - Maximum time allowed for sitemap generation.
Generate or Preview 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
php novakit sitemap --url=https://example.com --dry-runSet numeric crawl limits in the configuration file or pass a configured instance from PHP. The current Novakit command can reject numeric CLI arguments before it starts the crawl.
By default, the sitemap is written to public/sitemap.xml. A dry run makes crawl requests and skips writing the XML file.
Cron Execution in Production
To automatically generate sitemaps at 05:00 on alternating days of the month, configure a cron job like this:
# Run sitemap generation at 05:00 on alternating days of the month
0 5 */2 * * flock -n /tmp/task.lock /usr/bin/php /path/to/project/novakit sitemapExplanation:
0 5 */2 * *→ Runs at 05:00 on day 1, 3, 5, and so on, and restarts the pattern each month.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.
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. Run the generator from a CLI command or a CLI queue worker.
Task queue
Use a recurring database queue task to generate the sitemap at fixed intervals. Configure the database task queue before starting the worker.
// app/Tasks/TaskQueue.php
namespace App\Tasks;
use Luminova\Base\AbstractTaskQueue;
class TaskQueue extends AbstractTaskQueue
{
protected function tasks(): ?array
{
return [
[
'handler' => 'Luminova\Components\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)
]
];
}
}Initialize, enqueue, and run the task
php novakit task:init --class=TaskQueue
php novakit task:queue --class=TaskQueue
php novakit task:run --class=TaskQueueAutomating with cron in production
* * * * * /usr/bin/php /path/to/project/novakit task:run --class=TaskQueue --flock-workerUse
--flock-workerto prevent overlapping executions.
Using a Novakit Console Command
Register a custom Novakit command to generate a sitemap on demand.
// app/Console/SitemapCommand.php
namespace App\Console;
use Throwable;
use Luminova\Command\Terminal;
use Luminova\Components\Seo\Sitemap;
use Luminova\Base\Cli\AbstractConsole;
class SitemapCommand extends AbstractConsole
{
protected string $name = 'app-sitemap';
// Avoid conflicts with Luminova's built-in sitemap command
protected string $group = 'app-sitemap';
public function help(array $helps): int
{
return STATUS_ERROR;
}
public function run(?array $options = []): int
{
$url = $this->input->getAnyOption('url', 'u', APP_URL);
try {
if (Sitemap::generate($url, basename: 'sitemap.xml')) {
Terminal::success('Sitemap was successfully generated.');
return STATUS_SUCCESS;
}
Terminal::error('Failed to generate sitemap.');
} catch (Throwable $e) {
Terminal::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 --url=https://example.comUsing Routable Base Command
Define a routable CLI command on an AbstractController to run the generator through public/index.php.
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 Throwable;
use Luminova\Attributes\Group;
use Luminova\Command\Terminal;
use Luminova\Attributes\Command;
use Luminova\Components\Seo\Sitemap;
use Luminova\Base\Cli\AbstractController;
#[Group('sitemap')]
class SitemapCommand extends AbstractController
{
/**
* @var string $group Group name
*/
protected string $group = 'sitemap';
public function help(array $helps): int
{
return STATUS_ERROR;
}
/**
* Generates the sitemap for the website.
*
* @return int Status code: STATUS_SUCCESS or STATUS_ERROR
*/
#[Command('generator', group: 'sitemap')]
public function generator(): int
{
try {
$url = $this->option('url', 'u', APP_URL);
if (Sitemap::generate($url, basename: 'sitemap.xml')) {
Terminal::success('Sitemap was successfully generated.');
return STATUS_SUCCESS;
}
Terminal::error('Failed to generate sitemap.');
} catch (Throwable $e) {
Terminal::error('Error generating sitemap: ' . $e->getMessage());
}
return STATUS_ERROR;
}
}Run Routable command
Run the command from the project root with attribute routing enabled. The current grouped CLI dispatcher can fail before reaching the handler. The built-in Novakit command is available for sitemap generation.
php public/index.php sitemap generator --url=https://example.comConstants
| Constant | Value | Description |
|---|---|---|
GENERATE_SITEMAP | 1 | Generate an XML sitemap. |
Methods
generate
Crawl from a starting URL and write the XML sitemap to the application’s public/ directory by default. Use a configuration instance to override crawl limits and other application defaults.
public static function generate(
?string $url = null,
string $basename = 'sitemap.xml',
?AbstractConfig $config = null,
array $options = []
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string\|null | Start URL. Uses app.start.url when omitted (default: null). |
$basename | string | Output filename without directory separators (default: 'sitemap.xml'). |
$config | AbstractConfig\|null | Sitemap configuration, typically App\Config\Sitemap (default: null). |
$options | array{mode?: int, verbose?: int, isDryRun?: bool, ignoreAssets?: bool, treeTemplate?: string|null} | Output and crawl options (default: []). |
Return:
bool - True when generation succeeds, including a successful dry run. False when the crawl yields no results or the output cannot be written.
Throws:
- Luminova\Exceptions\RuntimeException If called outside the CLI, the basename contains a directory separator, or the start URL resolves to an empty path.
Use ['isDryRun' => true] to skip writing the file. XML generation is the default mode. See Links Audit for the other mode and shared option limitations.
Examples:
Generate an XML sitemap from a CLI script using the application configuration.
use Luminova\Components\Seo\Sitemap;
Sitemap::generate(url: 'https://example.com', basename: 'sitemap.xml');