TYPO3 9.5 中间件渲染页面模板

问题描述

我正在尝试从 Middleware 中呈现页面,因为它会在没有 Middleware 的情况下呈现,但我想更改内容(模板的主要部分)除外。

目前我正在使用 \TYPO3Fluid\Fluid\View\TemplateView 这样做。但是一旦 Layout 被呈现,其中包含网站标题、导航等。我在第 109 行的 Call to undefined method TYPO3Fluid\Fluid\Core\Rendering\RenderingContext::getControllerContext() 中收到错误 sysext/fluid/Classes/ViewHelpers/Link/PageViewHelper.PHP$uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();

我需要在我的 Context添加一个 View 还是有其他方法可以更改 Middleware 中的内容

中间件

<?PHP

    namespace Myvendor\MyExt\Middleware;

    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    use Psr\Http\Server\MiddlewareInterface;
    use Psr\Http\Server\RequestHandlerInterface;
    use TYPO3\CMS\Core\Core\Environment;
    use TYPO3\CMS\Core\Http\HtmlResponse;
    use TYPO3Fluid\Fluid\View\TemplateView;

    class CheckPassword implements MiddlewareInterface
    {
        protected $config = [
                'templatePathAndFilename' => 'EXT:myext/Resources/Private/Templates/PasswordProtection/Login.html','templateRootPaths'       => [0 => 'EXT:myext/Resources/Private/Templates/'],'partialRootPaths'        => [0 => 'EXT:myext/Resources/Private/Partials/'],'layoutRootPaths'         => [0 => 'EXT:myext/Resources/Private/Layouts/'],];

        private function responsetoMiddleware(ServerRequestInterface $request,RequestHandlerInterface $handler): ResponseInterface
        {
            return $handler->handle($request);
        }

        public function process(ServerRequestInterface $request,RequestHandlerInterface $handler): ResponseInterface
        {
                /** @var \TYPO3\CMS\Fluid\View\TemplateView $view */
                $view = $this->initializeStandaloneView();

                return new HtmlResponse($view->render());
        }

        /**
         * @param array|null $variables
         *
         * @return \TYPO3Fluid\Fluid\View\TemplateView
         */
        protected function initializeStandaloneView(array $variables = null): TemplateView
        {
            $view = new TemplateView();
            $paths = $view->getTemplatePaths();

            if (isset($this->config['templatePathAndFilename']) && is_array($this->config['templatePathAndFilename'])) {
                $templatePathAndFilename = str_replace('EXT:',Environment::getExtensionsPath() . '/',$this->config['templatePathAndFilename']);
                $paths->setTemplatePathAndFilename($templatePathAndFilename);
            }

            if (isset($this->config['layoutRootPaths']) && is_array($this->config['layoutRootPaths'])) {
                $layoutRootPaths = str_replace('EXT:',$this->config['layoutRootPaths']);
                $paths->setLayoutRootPaths($layoutRootPaths);
            }

            if (isset($this->config['templateRootPaths']) && is_array($this->config['templateRootPaths'])) {
                $templateRootPaths = str_replace('EXT:',$this->config['templateRootPaths']);
                $paths->setTemplateRootPaths($templateRootPaths);
            }

            if (isset($this->config['partialRootPaths']) && is_array($this->config['partialRootPaths'])) {
                $partialRootPaths = str_replace('EXT:',$this->config['partialRootPaths']);
                $paths->setPartialRootPaths($partialRootPaths);
            }

            $renderingContext = $view->getRenderingContext();
            $renderingContext->setControllerName('PasswordProtection');
            $renderingContext->setControllerAction('Login');
            $renderingContext->setTemplatePaths($paths);
            $view->setRenderingContext($renderingContext);

            if (isset($variables)) {
                $view->assignMultiple($variables);
            }

            return $view;
        }
    }

模板

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true" lang="en">
    <f:layout name="Page/Default"/>

    <f:section name="Main">
        Password Protection Content goes here
    </f:section>
</html>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)