Metadata Management
This document guides you through updating metadata for your PHP application using the MainLayout class. This ensures that each page has appropriate and specific metadata for SEO and user 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 is used to add additional metadata, such as the author of the page.
Adding Scripts to the Head and Footer
<?php
use Lib\MainLayout;
MainLayout::addHeadScript('<script src="head-script.js"></script>');
MainLayout::addFooterScript('<script src="footer-script.js"></script>');
?>
This example demonstrates how to add and output scripts for both the head and footer sections using the MainLayout class. The addHeadScript and addFooterScript methods ensure that scripts are included in the correct sections of your layout.
Dynamic Metadata for Specific Pages
The MainLayout class also supports setting dynamic metadata for specific pages based on conditions or query parameters.
<?php
use Lib\MainLayout;
use Lib\Request;
$productName = Request::$params->productName ?? '';
$productDescription = Request::$params->productDescription ?? '';
MainLayout::$title = 'Product: ' . $productName;
MainLayout::$description = $productDescription;
?>
Here, the metadata for a product page is dynamically set using the product name and description passed as query parameters. This ensures that the page metadata accurately reflects the product being viewed.
Important Note
Ensure that metadata is set at the top of your PHP file before any HTML output to ensure proper rendering in the HTML head section.
By following this approach, you ensure your metadata is contextually accurate, enhancing both SEO and user experience.