You first need to create a controller to define a route.
The controller is responsible for configuring the route and passing data to the template.
All backend logic related to this route should be placed inside this controller.
However, you cannot place the controller file just anywhere.
It must be located at the following path in your extension: EXT:/Classes/Controller(/optional/subdirs)
Every controller file must use the Controller suffix before the .php extension, and the class name must match the file name.
A valid controller file path could be: project/packages/extension/Classes/Controller/MyController.php
The declaration of the class should look like:
<?php
namespace Vendor\Extension\Controller;
class myController
{
...
}
Next, your class must extend EasyController.
<?php
namespace Vendor\Extension\Controller;
class myController extends EasyController
{
...
}
Easy TYPO3 Routes uses PHP attribute syntax to declare routes.
This approach is inspired by Symfony, but uses different properties.
A route attribute is declared as follows:
#[Route('/PATH/TO/THE/PAGE')]
The first parameter is always the path and is mandatory.
Alternatively, you can use a named parameter:
#[Route(path: '/PATH/TO/THE/PAGE')]
The attribute must be placed directly above the method that handles the request, performs the logic, and returns the response.
#[Route(path: '/path/to/the/page')]
public function showPage(): ?ResponseInterface
{
...
}
The method itself must return either a ResponseInterface instance or null.
Use EasyController’s render method to create and return the response.
Note: The method name can be chosen freely and does not affect the functionality, but it should be selected carefully for readability and maintainability.
The controller automatically handles the request.
Note: You can access the request also manually.
$request = $this->request;
To create and return a response use the inherited function 'render'.
return $this->render('EXT:extension/Resources/Private/PageView/Pages/Subfolder/Template.html', ['variable1' => 1, 'variable2' => 'test'], partialRootPaths: ['EXT:extension/Resources/Private/PageView/Partials']);
The first parameter is the path to the template to render and return, the second is an array of variables to pass to Fluid.
Reference the Functions documentation to learn more about the 'render' function.
Important: Due to TYPO3’s architecture, the page must exist in the page tree and match the route path. Otherwise, a 404 exception will be thrown.
You can bypass this requirement by setting the bypass404 attribute to true.
Every method in a controller can represent a page.
<?php
namespace Ds\Testpage\Controller;
use Ds\Easy\Attribute\Route;
use Ds\Easy\Controller\EasyController;
use Psr\Http\Message\ResponseInterface;
final class TestController extends EasyController
{
#[Route('/test', format: 'html')]
public function showPage(): ?ResponseInterface
{
return $this->render('EXT:testpage/Resources/Private/PageView/Pages/Test.html');
}
#[Route('/test2')]
public function showOtherPage(): ?ResponseInterface
{
return $this->render('<h1>Yet another page</h1>');
}
}You can define and use TYPO3 data processor classes to pass data to Fluid templates.
The example below shows how to pass a navigation menu to Fluid using such a processor.
<?php
namespace Vendor\Extension\Controller;
use Ds\Easy\Attribute\Route;
use Ds\Easy\Controller\EasyController;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\DataProcessing\MenuProcessor;
class DemoController extends EasyController
{
#[Route(path: '/path/to/the/page')]
public function showPage(): ?ResponseInterface
{
$mp = GeneralUtility::makeInstance(MenuProcessor::class);
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$cObj->setRequest($this->request);
$nav = $mp->process($cObj, [], ['levels' => 3, 'as' => 'nav', 'special' => 'directory', 'special.' => ['value' => 13]], []);
return $this->render('EXT:extension/Resources/Private/PageView/Pages/Demo/Page.html', ['nav' => $nav['nav']], partialRootPaths: ['EXT:extension/Resources/Private/PageView/Partials']);
}
}
For more details on working with data processors in PHP, refer to the official TYPO3 documentation.
Finally, run
typo3 cache:flush
once to create the required cache file.
The cache file is deleted whenever caches are cleared, either by running the console command or by clearing the frontend and/or backend cache in the TYPO3 backend.
After the initial typo3 cache:flush, the corresponding Easy cache file is recreated automatically when a page is visited.