Skip to content

Page

A Page is a Symfony entity that represents a single page in the database. It is located at src/Entity/Page.php.

Pages have their own properties such as title, slug, and enabled status, and are associated with a PageType. Each page can have multiple sections assigned to it.


Mutating Page Data

All SectionType controllers have access to the shared Page entity. The page is compiled only after all sections have been processed, which means that you can mutate the page data in any section and it will be reflected in the final output.

Changing The Page Title In a Section

<?php

// ...

class SomeSectionType extends AbstractSection
{

    // ...

    final public function handlePageSectionRequest(
        Request $request,
        Page $page,
        Section $section,
        PageParameters $pageParameters
    ): Response {
        // the title of the final result will be "My New Title"
        $page->setTitle('My New Title');
        return $this->render(/* ... */)
    }
}

Creating a Page In Code

Sometimes, you might want a page to be created in code and not by the user via the administration panel. For example, this is useful for if you want to leverage the page builder to grab the header and footer but then fill in the content yourself.

This is an exampe showing how to create a Page entity and leverage the PageBuilder service to wrap your output with the website's header and footer:

Example Code-Only Page

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use App\Repository\PageTypeRepository;
use App\Controller\SectionTypesBuiltin\FooterSection;
use App\Controller\SectionTypesBuiltin\HeaderSection;
use App\Entity\Page;
use App\Entity\PageType;

class SomeController extends AbstractController
{
    public function __construct(
        private PageBuilder $pageBuilder,
        private PageTypeRepository $pageTypeRepository,
        private PageParameters $pageParameters,
    ) {}
    )

    private function getHeaderAndFooter(Request $request, ?Page $page): array
    {
        $header = $this->forward(HeaderSection::class . '::handlePageSectionRequest', [
            'request' => $request,
            'page' => $page,
            'pageParameters' => $this->pageParameters,
        ])->getContent();

        $footer = $this->forward(FooterSection::class . '::handlePageSectionRequest', [
            'request' => $request,
            'page' => $page,
            'pageParameters' => $this->pageParameters,
        ])->getContent();

        return [$header, $footer];
    }

    public function someResponseMethod(Request $request): Response
    {
        // we provide slug and url for breadcrumb compatibility
        $page = (new Page())
            ->setTitle('My New Page')
            ->setSlug('my-new-page')
            ->setPageURL('/my-new-page')
            ->setEnabled(true)
            ->setPageType(
                $this->pageTypeRepository->findOneBy(['name' => 'Web'])
            );
        $this->pageBuilder->initPage($page);

        [$header, $footer] = $this->getHeaderAndFooter($request, $page);
        $this->pageBuilder->setHeaderHTML($header);
        $this->pageBuilder->setFooterHTML($footer);

        $this->pageBuilder->setBodyHTML("<strong>Hello World!</strong>");

        // will return a web page with "Hello World!" in the body, wrapped
        // with the header and footer sections.
        return $this->pageBuilder->getCompiledResponse();
    }
}