使用中间件和 URL 模式处理 TYPO3 extbase 控制器操作

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

我需要一些带有 TYPO3 中间件的简单解决方案,它可以识别 URL 模式并相应地返回 json 响应。

URL 模式:https://example.com/[api]/[ext_name]/[controllerName]/[actionName]

例如:https://example.com/api/event/eventManagement/list

根据此 URL,它应该调用控制器操作。我按照文档示例编写中间件,但它抛出错误。

下面的代码片段也有同样的错误。

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;

/**
 * This middleware can be used to retrieve a list of seasons with their according translation.
 * To get the correct translation the URL must be within a base path defined in site
 * handling. Some examples:
 * "/en/haiku-season-list.json" for English translation (if /en is the configured base path)
 * "/de/haiku-season-list.json" for German translation (if /de is the configured base path)
 * If the base path is not available in the according site the default language will be used.
 */
final class EventApiResolver implements MiddlewareInterface
{
    public function __construct(
        private readonly LanguageServiceFactory $languageServiceFactory,
        private readonly ResponseFactoryInterface $responseFactory,
        private readonly StreamFactoryInterface $streamFactory
    ) {}
}

我想用中间件处理 GET、POST、DELETE、PUT 请求。有没有关于在没有第三方扩展的情况下创建它的提示?

使用中间件剩余 URL 处理 Extbase 控制器操作的一些解决方案

typo3 middleware extbase typo3-extensions
1个回答
0
投票

我建议阅读官方文档的以下部分:https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ApiOverview/DependencyInjection/Index.html#public

为了完整性粘贴在这里:

直接访问包括通过 GeneralUtility::makeInstance() 和构造函数参数进行实例化。

下一节还提到了您的错误:

https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ApiOverview/DependencyInjection/Index.html#errors-resulting-from-wrong-configuration

如果使用依赖注入的对象没有正确配置, 可能会导致以下一个或多个问题。在这种情况下,请检查 类是否必须配置为 public:true。

ArgumentCountError 因缺少依赖注入而引发 构造函数注入:

我敢打赌你的中间件在 DI 中没有标记为公共,将其标记为公共可以解决问题吗?也许您还可以从堆栈跟踪中添加更多上下文?这应该揭示了实例是如何创建的。

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