Section
A Section
is a Symfony entity that represents a single section in the database. It
is located at src/Entity/Section.php
.
Sections have their own properties such as title
, content
, and enabled
status,
and each section entity exists only on a single page. A section also stores JSON
data that contains the configuration done for it in the administration panel.
Getting Section HTML By Forwarding The Request
If you need only a section's HTML content, you can forward the request to the appropriate section controller and feed it the correct paramters. Doing so will result in the HTML content that the section would render:
Example Fetching Section HTML
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Page;
use App\Service\PageParameters;
use App\Controller\SectionTypes\PageTitle\PageTitleSection;
class SomeController extends AbstractController
{
public function __construct(
private PageParameters $pageParameters,
) {}
public function someResponseMethod(Request $request): Response
{
$titleSectionHTML = $this->forward(PageTitleSection::class . '::handlePageSectionRequest', [
'request' => $request,
'page' => (new Page())
->setTitle('My Page Title')
->setSlug('my-page-title')
->setEnabled(true),
'pageParameters' => $this->pageParameters,
])->getContent();
// do whatever you want with $titleSectionHTML
}
}
Creating A Section In Code
A section can be created in code by instantiating the Section
entity with
the required properties. In this example, we'll create a title section in
code and add it to a page
Example Title Section Type
<?php
use App\Entity\Section;
use App\Repository\SectionTypeRepository;
class SomeController extends AbstractController
{
public function __construct(
SectionTypeRepository $sectionTypeRepository,
) {}
public function someResponseMethod(Request $request): Response
{
$section = (new Section())
->setPage($someDefinedPageEntity)
->setTitle('My Section Title')
->setSectionType(
$this->sectionTypeRepository->findOneBy(['name' => 'Page Title'])
)
->setEnabled(true);
; // set other properties as needed
// ...
}
}
- Next Up: PageType Controllers