Metadata Management
This document guides you through updating metadata for your PHP application using the
MainLayout class. This ensures each page has
appropriate and specific metadata for SEO and navigation purposes.
Adding Metadata with MainLayout Class
To add or update metadata, use the methods provided in the
MainLayout class. This allows you to manage head
scripts, footer scripts, and custom metadata dynamically for each page.
Example: Setting Metadata for a Page
<?php
use Lib\MainLayout;
MainLayout::$title = "My Awesome Page";
MainLayout::$description = "This is a description of my awesome page";
MainLayout::addCustomMetaData("author", "John Doe");
?>
In this example, the page’s title and description are dynamically set using the
MainLayout class properties. The
addCustomMetaData method adds additional metadata
such as the page author.
Adding Scripts to the Head and Footer
The example below demonstrates how to add scripts to both the head and footer elements using
the MainLayout class.
<?php
use Lib\MainLayout;
MainLayout::addHeadScript('<script src="head-script.js"></script>');
MainLayout::addFooterScript('<script src="footer-script.js"></script>');
?>
Dynamic Metadata for Specific Pages
The MainLayout class also supports dynamically
setting metadata based on query parameters or conditions.
<?php
use Lib\MainLayout;
use Lib\Request;
$productName = Request::$params->productName ?? '';
$productDescription = Request::$params->productDescription ?? '';
MainLayout::$title = "Product: " . $productName;
MainLayout::$description = $productDescription;
?>
Here, metadata for a specific product page is dynamically generated based on the product name and description received through query parameters.
<head> section.
By following this approach, your metadata stays contextually accurate, improving SEO and enhancing user experience.