Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Implement and use a core ViewFactoryInterface #4747

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ The sequence looks like the following:
objects (later created implicitly in this example) are organized in the
:php:`\TYPO3\CMS\Core\Context\SecurityAspect`.

.. versionchanged:: 13.3
linawolf marked this conversation as resolved.
Show resolved Hide resolved
Use the :ref:`generic-view-factory` to create a view, previously
used :php:`TYPO3\CMS\Fluid\View\StandaloneView` is deprecated.

.. literalinclude:: _CSRFlikeRequestTokenHandling/_MyController.php
:caption: EXT:my_extension/Classes/Controller/MyController.php

Expand Down
56 changes: 52 additions & 4 deletions Documentation/ApiOverview/Fluid/UsingFluidInTypo3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,58 @@ Here are some examples of how Fluid can be used in TYPO3:
creation of a sitepackage extension.
* :ref:`adding-your-own-content-elements` in addition to the already existing
content elements TYPO3 supplies.
* The previous point describes the lightweight components which
are created using a combination of TypoScript and Fluid. If you need more
functionality or flexibility in your content element, you can create a
content plugin using a combination of Extbase and Fluid.
* :ref:`Extbase-based controllers <extbase-controller>` have a default Fluid
view in :php:`$this->view`.
* Use Fluid to create emails using the :ref:`TYPO3 Mail API <mail-fluid-email>`.
* Use Fluid in :ref:`backend modules <backend-modules-template>`, either with or
without Extbase.
* Use the :ref:`generic view factory <generic-view-factory>` to create a
Fluid view.

.. deprecated:: 13.3
These classes have been marked as deprecated in TYPO3 v13 and will be removed in v14:

* :php:`TYPO3\CMS\Fluid\View\StandaloneView`
* :php:`TYPO3\CMS\Fluid\View\TemplateView`
* :php:`TYPO3\CMS\Fluid\View\AbstractTemplateView`
* :php:`TYPO3\CMS\Extbase\Mvc\View\ViewResolverInterface`
* :php:`TYPO3\CMS\Extbase\Mvc\View\GenericViewResolver`

.. _generic-view-factory:

Using the generic view factory (ViewFactoryInterface)
=====================================================

.. versionadded:: 13.3
Class :php:`TYPO3\CMS\Core\View\ViewFactoryInterface` has been added as a
generic view factory interface to create views that return an instance of
:php:`TYPO3\CMS\Core\View\ViewInterface`. This implements the "V" of "MVC"
in a generic way and is used throughout the TYPO3 core.

You can :ref:`inject <dependency-injection>` an instance of the
:php:`TYPO3\CMS\Core\View\ViewFactoryInterface` to create an instance of a
:php:`TYPO3\CMS\Core\View\ViewInterface` where you need one.

.. note::
:ref:`Extbase-based controllers <extbase-controller>` create a view
instance based on this factory by default and which is accessible as
:php:`$this->view`.

.. literalinclude:: _UsingFluid/_MyController.php
:caption: EXT:my_extension/Classes/Controller/MyController.php
linawolf marked this conversation as resolved.
Show resolved Hide resolved
linawolf marked this conversation as resolved.
Show resolved Hide resolved

The :php-short:`TYPO3\CMS\Core\View\ViewFactoryInterface` needs an instance of
:php:`\TYPO3\CMS\Core\View\ViewFactoryData`, which is a data object and should
therefore be created via :php:`new`.

Best practices in creating a :php-short:`\TYPO3\CMS\Core\View\ViewFactoryData`
instance:

* Hand over request of type :php:`\Psr\Http\Message\ServerRequestInterface`
if possible. See :ref:`getting-typo3-request-object`.
* Use the tuple `$templateRootPaths`, `$partialRootPaths` and
`$layoutRootPaths` if possible by providing an array of "base" paths
like `'EXT:my_extension/Resources/Private/(Templates|Partials|Layouts)'`
* Avoid using parameter `$templatePathAndFilename`
* Call `render('path/within/templateRootPath')` without file-ending on the
returned ViewInterface instance.
27 changes: 27 additions & 0 deletions Documentation/ApiOverview/Fluid/_UsingFluid/_MyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MyVendor\MyExtension\Controller;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\View\ViewFactoryData;
use TYPO3\CMS\Core\View\ViewFactoryInterface;

final readonly class MyController
{
public function __construct(
private ViewFactoryInterface $viewFactory,
) {}

public function myAction(ServerRequestInterface $request): string
{
$viewFactoryData = new ViewFactoryData(
templateRootPaths: ['EXT:my_extension/Resources/Private/Templates'],
partialRootPaths: ['EXT:my_extension/Resources/Private/Partials'],
layoutRootPaths: ['EXT:my_extension/Resources/Private/Layouts'],
request: $request,
);
$view = $this->viewFactory->create($viewFactoryData);
$view->assign('mykey', 'myValue');
return $view->render('path/to/template');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ The TYPO3 request object is an implementation of the PSR-7 based
.. contents::
:local:


.. _getting-typo3-request-object:

Getting the PSR-7 request object
Expand Down
Loading