[英]How can I overwrite a controller function of a typo3 extension?
I have a Typo3 9 application that uses several extensions.我有一个使用多个扩展的 Typo3 9 应用程序。 One of them is the "main" extension and includes various resource files, models, controllers and so on.其中之一是“主要”扩展,包括各种资源文件、模型、控制器等。 Because this extension is developed seperately and I might have to update it later, I don't want to touch its code.因为这个扩展是单独开发的,我以后可能要更新它,所以我不想碰它的代码。 For that reason, I created a separare extension, which already works well to overwrite templates and stylings.出于这个原因,我创建了一个单独的扩展,它已经可以很好地覆盖模板和样式。 However I can't really figure out how to cleanly overwrite controller functions.但是我真的不知道如何干净地覆盖 controller 函数。
Example: The main extension has a controller called "FrontendController.php".示例:主扩展有一个名为“FrontendController.php”的 controller。 This controller has a function called "blogDetailAction", which is called when accessing the detail view of a blog entry.这个 controller 有一个名为“blogDetailAction”的 function,它在访问博客条目的详细视图时被调用。 I want to change/overwrite this function by unsing my extension.我想通过取消扩展名来更改/覆盖此 function。 How would I do that?我该怎么做?
Thank you!谢谢!
If you want to change the code of a class which doesn't provide options like hooks or events , you have to use the XClass (Extending Class) mechanism.如果要更改不提供钩子或事件等选项的 class 的代码,则必须使用 XClass(扩展类)机制。
To do that, in your own extension, file EXT:YourExtension/ext_localconf.php, add this kind of code:为此,在您自己的扩展名文件 EXT:YourExtension/ext_localconf.php 中,添加以下代码:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][VendorName\ExtensionName\Controller\FrontendController::class] = [
'className' => YourVendorName\YourExtensionName\Xclass\NewFrontendController::class
];
Then in your NewFrontendController.php:然后在你的 NewFrontendController.php 中:
class NewFrontendController extends \VendorName\ExtensionName\Controller\FrontendController
{
// Your code here
}
Every class created with \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() can be extended with XClass mechanism.使用\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance()创建的每个 class 都可以使用 XClass 机制进行扩展。
But you have to know that using XClass is risky because: neither the core, nor extensions authors can guarantee that XCLASSes will not break if the underlying code changes.但是您必须知道使用 XClass 是有风险的,因为:无论是核心作者还是扩展作者都不能保证 XCLASS 不会在底层代码更改时中断。
More information here: https://docs.typo3.org/m/typo3/reference-coreapi/9.5/en-us/ApiOverview/Xclasses/Index.html更多信息在这里: https://docs.typo3.org/m/typo3/reference-coreapi/9.5/en-us/ApiOverview/Xclasses/Index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.