BackendModule 上的 Typo3 依赖注入不起作用

问题描述 投票:0回答:1

我正在尝试创建一个基于

Extbase
的后端模块,如下所述:https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ExtensionArchitecture/HowTo/BackendModule/CreateModuleWithExtbase .html

我收到以下错误:

Too few arguments to function Peni4142\ImageCompressor\Controller\ImageCompressorController::__construct(), 0 passed in /var/www/html/vendor/typo3/cms-core/Classes/Utility/GeneralUtility.php on line 2961 and exactly 1 expected

似乎依赖注入不起作用,但我不知道。

你有什么提示吗?

堆栈跟踪:

in /var/www/html/ext/image_compressor/Classes/Controller/ImageCompressorController.php line 14

at Peni4142\ImageCompressor\Controller\ImageCompressorController->__construct()
in /var/www/html/vendor/typo3/cms-core/Classes/Utility/GeneralUtility.php line 2961

at TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Peni4142\\ImageCompressor\\Controller\\ImageCompressorController')
in /var/www/html/vendor/typo3/cms-core/Classes/Http/Dispatcher.php line 86

控制器

<?php

declare(strict_types=1);

namespace Peni4142\ImageCompressor\Controller;

use TYPO3\CMS\Backend\Attribute\Controller;
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

#[Controller]
final class ImageCompressorController extends ActionController
    {
     public function __construct(
         protected readonly ModuleTemplateFactory $moduleTemplateFactory,
     ) {

     }

     // use Psr\Http\Message\ResponseInterface
     public function handleRequest(): ResponseInterface
     {
         $this->view->assign('someVar', 'someContent');
         $moduleTemplate = $this->moduleTemplateFactory->create($this->request);
         // Adding title, menus, buttons, etc. using $moduleTemplate ...
         $moduleTemplate->setContent($this->view->render());
         return $this->htmlResponse($moduleTemplate->renderContent());
     }
}

配置/后端/Modules.php

<?php

use Peni4142\ImageCompressor\Controller\ImageCompressorController;

return [
    'image_compressor_config' => [
        'parent' => 'file',
        'position' => ['after' => '*'],
        'access' => 'admin',
        // 'path' => '/module/system/permissions',
        'extensionName' => 'Image Compressor',
        'iconIdentifier' => 'module-permission',
        'navigationComponent' => '@typo3/backend/page-tree/page-tree-element',
        'labels' => [
            'title' => 'Image Compressor',
            'description' => 'Compressing images',
            'shortDescription' => 'Creating compressed images and stores compressing Template'
        ],
        'aliases' => ['system_BeuserTxPermission'],
        'routes' => [
            '_default' => [
                'target' => ImageCompressorController::class . '::handleRequest',
            ],
        ],
    ],
];

部分composer.json

  "require": {
    "typo3/cms-core": "^12",
    "typo3/cms-backend": "^12",
    "typo3/cms-extbase": "^12",
    "typo3/cms-rte-ckeditor": "^12",
    "typo3/cms-fluid-styled-content": "^12"
  },
  "autoload": {
    "psr-4": {
      "Peni4142\\ImageCompressor\\": "Classes/"
    }
  },
  "replace": {
    "typo3-ter/personal": "self.version"
  },
  "extra": {
    "typo3/cms": {
      "extension-key": "image_compressor"
    }
  }

因为我是 PHP 新手,所以我不得不说我使用文本编辑器来创建 Composer 文件;我没有使用任何命令将它们添加到作曲家文件中。

感谢您的宝贵时间。

php typo3
1个回答
0
投票

请查看回溯第 86 行的

in /var/www/html/vendor/typo3/cms-core/Classes/Http/Dispatcher.php
。正如你所看到的,TYPO3 将首先尝试使用 Symfony DI/Container 实例化你的类,然后再尝试
GeneralUtility::makeInstance
方式。

因此,由于 Symfony DI 无法在 Symfony 容器中找到您的控制器,我非常确定您的控制器位于

getRemovedIds
文件的
var/cache/code/di/DependencyInjection_[hash].php
部分。

由于您已经用属性

#Controller
标记了控制器,并且扩展
ActionController
看起来是正确的,看来您忘记了在TYPO3的InstallTool中刷新缓存。该按钮需要几秒钟,因为它还会在缓存中重新创建 DependencyInjection 文件。刷新缓存后,您的控制器应该位于
methodMap
部分,并且应该按预期工作。

史蒂芬

© www.soinside.com 2019 - 2024. All rights reserved.