PHP Luminova: Generating Sitemaps & Embedding Structured Data on Webpages
Configure sitemap generation and add structured data to page responses.
A sitemap lists the public URLs you want search engines to discover. Structured data describes the content of individual pages. Configure each independently, then inspect the generated sitemap or rendered HTML. See the Embedding Structured Data and SEO Metadata.
Sitemap Example
Sitemaps are typically generated using the NovaKit command-line tool. Before generating your sitemap, you'll need to configure basic settings such as defining URL patterns to ignore, setting the application's start URL,specifying the maximum scan depth, and optionally configuring route patterns to capture the last modified date of pages.
Sitemap Configuration
To set up your sitemap, you'll need to edit the configuration file located at /app/Config/Sitemap.php. For more detailed guidance, please refer to the Sitemap Generator Configuration documentation.
// /app/Config/Sitemap.php
namespace App\Config;
use Luminova\Base\AbstractConfig;
final class Sitemap extends AbstractConfig
{
/**
* Maximum depth for scanning URLs.
* Set to 0 for unlimited scanning.
*/
public int $maxScan = 0;
/**
* The start URI prefix set to blank string to scan from start url.
*/
public string $startPrefix = '';
/**
* Patterns of URLs to ignore during the scan.
*/
/**
* @var string[]
*/
public array $ignoreUrls = [
'*/blog/edit/*',
'*/admin/*',
'*/foo/*'
];
/**
* Patterns to identify pages and determine their last modified timestamps.
*
* @var array<string, string> $viewUrlPatterns
*/
public array $viewUrlPatterns = [
'index' => '/',
'about' => '/about',
'blogs' => '/blog',
'blog' => '/blog/(:number)'
];
}Start URL Configuration
To set your application's start URL, edit the environment variables file. Locate or add the dev.app.start.url entry, uncomment it if necessary, and set it to the correct URL for your application.
For production environments, use your live domain:
dev.app.start.url = https://example.com/If you're working in a development environment, the URL will depend on where your project is located:
Project within a subdirectory: If your project is in a subdirectory within the server root (e.g.,
htdocsorwwwon servers like XAMPP or WAMPP):dev.app.start.url = https://localhost/example.com/public/Project in the document root: If your project is directly in the document root:
dev.app.start.url = https://localhost/public/Using Luminova's built-in server: If you're using the built-in PHP and Luminova development server running on
localhostwith port8080:dev.app.start.url = https://localhost:8080/Custom hostname and port: If you're running the Luminova development server with a custom hostname and port, specify it as follows:
dev.app.start.url = https://your-host-name:port/
Once your setup is complete, navigate to your application's root directory where the novakit file is located and run the following command:
php novakit sitemapSchema Example
The Schema Generator allows you to define default settings that can be overridden on a per-page basis.To configure the default options, edit the file located at /app/Config/Schema.php.For a deeper understanding, refer to the Schema Structured Data Configuration documentation.
Schema Setup
To start using the Schema implementation in your web pages, initialize the Schema class instance within your application's controller class.
Here's an example using the application's __construct method:
// /app/Application.php
namespace App;
use Luminova\Components\Seo\Schema;
use Luminova\Foundation\Core\AbstractApplication;
class Application extends AbstractApplication
{
/**
* @var Schema $schema
*/
protected ?Schema $schema = null;
public function __construct()
{
$this->schema = new Schema();
parent::__construct();
// Set your preferred canonical version
//$this->schema->setCanonical(APP_WWW_URL, $this->getView()); // Using www version
$this->schema->setCanonical(APP_URL, $this->getView());
}
}Alternatively, you can use the onCreate method:
// /app/Application.php
namespace App;
use Luminova\Components\Seo\Schema;
use Luminova\Foundation\Core\AbstractApplication;
class Application extends AbstractApplication
{
/**
* @var Schema|null $schema
*/
public ?Schema $schema = null;
public function onCreate()
{
$this->schema = new Schema();
$this->schema->setUrl(APP_URL, $this->getView());
}
}Using Schema in Views
After setting up the Schema in your application controller, you can include it in your views where you need the schema object.
Overriding Default Configuration
// /app/Controllers/Http/BlogController.php
namespace App\Controllers\Http;
use App\Models\BlogModel;
use Luminova\Base\Http\AbstractController;
class BlogController extends AbstractController
{
public function blog(string $id, BlogModel $blog): int
{
$this->app->schema->setObject([
'isArticle' => true,
'article' => [
'url' => $this->request->getUrl(),
'headline' => 'This is the page summary',
'description' => 'This is the page description',
'title' => 'This page title',
'keywords' => ['Luminova', 'PHP'],
'word_count' => 28,
]
// more options...
]);
return $this->view('blog', [
'content' => $blog->find($id)
]);
}
}Finalize Schema Setup
Now within your template view file in /resources/Views/, call the schema methods getHeadTags, getJsonLdScript and getTitle to finalize the setup.
<!-- /resources/Views/blog.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<?= $this->app->schema->getHeadTags() . $this->app->schema->getJsonLdScript();?>
<title><?= $this->app->schema->getTitle();?></title>
</head>
<body class="blog-page">
<div class="container">
<h1>Your Blog Title</h1>
<p>Your Blog Body</p>
</div>
</body>
</html>