Chapter 17: A Centralised SEO and Metadata Strategy

Introduction: Beyond Hardcoded Tags

In many web applications, SEO-related tags like <title> and <meta name="description"> are often hardcoded directly into the header templates. While simple, this approach quickly becomes unmanageable, especially in a multi-site platform. A change to a page's title requires a code deployment, and there is no central place to manage the metadata for the entire site.

This chapter details a more powerful, centralised, and database-driven approach. We will show how our architecture uses a single database table as the source of truth for all page metadata, and how dedicated "Presenter" classes are used to generate both the standard HTML meta tags and the advanced JSON-LD structured data that is crucial for modern SEO.


A Single Source of Truth: The httpRequests Table

The foundation of our centralised metadata strategy is the httpRequests table. This table acts as a master map for every possible page on every site within our unified application. Instead of scattering SEO information across dozens of template files, we define it all in one structured location. For each page request, this table stores the critical metadata needed for the <head> section of the HTML document.

Key columns include:

  • pageTitle: The content for the <title> tag.
  • pageDescription: The content for the <meta name="description"> tag.
  • canonical: The canonical URL for the page, preventing duplicate content issues.
    • schemaTypes: A list of JSON-LD schema types that should be generated for this page.

By storing this information in the database, an administrator could, with a secure interface, update page titles and descriptions across the entire platform without ever needing to edit a .tpl file or request a code deployment.


The MetaPresenter Class

To keep our logic clean and centralised, we use a dedicated MetaPresenter class. This class is a simple service whose only job is to take the final metadata and build the complete block of HTML meta tags for the <head> section of our document. The baseController instantiates this class after all the models have run and the final page title and description have been determined.

<?php
class MetaPresenter
{
    private array $metaTags = [];

    public function __construct(string $pageTitle, string $description, string $canonicalUrl)
    {
        // Standard meta tags
        $this->addTag('meta', ['charset' => 'UTF-8']);
        $this->addTag('meta', ['name' => 'viewport', 'content' => 'width=device-width, initial-scale=1.0']);
        $this->addTag('meta', ['name' => 'description', 'content' => $description]);

        // Open Graph tags for social media
        $this->addTag('meta', ['property' => 'og:title', 'content' => $pageTitle]);
        $this->addTag('meta', ['property' => 'og:description', 'content' => $description]);

        // Canonical link tag
        $this->addTag('link', ['rel' => 'canonical', 'href' => $canonicalUrl]);
    }

    // ... (helper methods) ...
}

Enhancing Visibility with JSON-LD Structured Data

While standard meta tags are important, Structured Data is a modern SEO technique that gives search engines a much deeper, more explicit understanding of your page's content. We provide this information using the JSON-LD format, which is Google's recommended approach.

JSON-LD is a block of JavaScript code that contains a structured summary of the page's content, written in a standardised vocabulary from schema.org. This allows us to tell Google, for example, "This page is a TechArticle, its headline is 'X', and it was published on date 'Y'." By providing this unambiguous data, we make our pages eligible for Rich Snippets in search results.

The SchemaPresenter Class

To manage the creation of this JSON-LD, we use another dedicated service class: the SchemaPresenter. Its job is to take data from our models and build a valid, structured data array that is ready to be JSON-encoded and placed in the of the document. The class is designed to be flexible, able to generate different schema types (like WebSite or TechArticle) based on the data provided. PHP

<?php
class SchemaPresenter
{
    private array $schema = [];

    public function __construct(string $type, string $headline, string $url)
    {
        $this->schema['@context'] = '[https://schema.org](https://schema.org)';
        $this->schema['@type'] = $type;
        $this->schema['headline'] = $headline;
        $this->schema['url'] = $url;
    }

    public function setAuthor(string $name): void
    {
        $this->schema['author'] = [
            '@type' => 'Person',
            'name' => $name
        ];
    }

    public function build(): array
    {
        return $this->schema;
    }
}

Conclusion: A Maintainable SEO Foundation

By centralising all our page metadata in the database and using dedicated "Presenter" classes, we create a system that is powerful, flexible, and easy to maintain. The MetaPresenter ensures that all standard HTML tags are generated consistently, while the SchemaPresenter provides the rich, structured data that modern search engines require.

Shopping summary

Item

SubTotal: £

Empty Basket

this is a hidden panel

Completed in 43 milliseconds

Completed in 43 milliseconds

This is the top Panel

This is the bottom Panel