Typo3 自定义扩展路由增强器

问题描述

我使用 Extension Builder 扩展创建了一个自定义 Typo3 v9.5.26 扩展。

我的扩展程序位于typo3conf/ext/xyz_sortitems 并对项目进行排序。

一个项目具有属性 itemid、名称、日期和数量

我的 ext_localconf.PHP 看起来像这样:

<?PHP  
if (!defined('TYPO3_MODE')) {  
    die ('Access denied.');  
}  
  
call_user_func(  
    function() {  
  
        \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(  
            'XYZ.XyzSortitems','Sortitems',// cacheable actions  
            ['Sortitems' => 'sortbyname,sortbydate,sortbyamount,showsingleitem'],// non-cacheable actions  
            ['Sortitems' => 'sortbyname,showsingleitem']  
        );  
  
    }  
);

我的视图控制器位于 Typ3conf/ext/xyz_sortitems/classes/Controller/SortitemsController.PHP 中,看起来像这样:

<?PHP  
  
namespace XYZ\XyzSortitems\Controller;  
  
class SortitemsController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {  
  
    protected $sortitemsRepository = null;  
  
    public function injectSortitemsRepository(\XYZ\XyzSortitems\Domain\Repository\SortimtesRepository $sortitemsRepository) {  
        $this->sortitemsRepository = $sortitemsRepository;  
    }  
  
    public function sortbynameAction() {  
        // sorts items by name here...  
    }  
  
    public function sortbydateAction() {  
        // sorts items by date here...  
    }  
  
    public function sortbyamountAction() {  
        // sorts items by amount here...  
    }  
  
    public function showsingleitemAction() {  
        // displays a single item here...  
    }  
  
}

...我的视图模板(例如 sortbydateAction)位于 ext/xyz_sortitems/Resources/Private/Templates/Sortbydate.html 并生成指向相应项目的链接,如下所示:

<html xmlns:f="http://typo3.org/ns/fluid/ViewHelpers">  
    <f:layout name="defaultLayout" />  
    <f:section name="sectionname">  
        <f:for each="{items}" as="item">  
  
            <!-- here the link to the single item page is generated -->  
            <f:link.action action="showsingleitem" pageUid="4321" arguments="{itemid: item.itemid}">  
  
        </f:for>  
    </f:section>  
</html>

我的扩展程序按预期工作。

现在,当我查看显示项目排序列表(例如按日期排序)的页面的前端时,Fluid 模板引擎会生成此列表中项目到相应目标页面链接(每个页面显示一个单项)大概是这样的:

http://example.com/item  
?tx_xzysortitems_sortitems%5Baction%5D=showsingleitem 
&tx_xzysortitems_sortitems%5Bcontroller%5D=Sortitems  
&tx_xzysortitems_sortitems%5Bitemid%5D=12345  
&cHash=38a2dd43d7b0c4997c3b0ff6d4430e55

相反,我需要指向相应页面链接显示单个项目,如下所示:

http://example.com/item/{itemid}/{name}  
(e.g. http://example.com/item/12345/ice-cream-on-toast)

由于 RealURL 在 Typo2 v9 中消失了,我显然需要在 Typo3conf/ext/xyz_sortitems/config.yaml 中使用路由增强器,从 Typo3 数据库中的扩展表中读取 itemid 和 name,并将这些值放入指向单项页面

不幸的是,大约 90% 的代码示例我都可以找到参考 Georg Ringer 的基于 pi 的“新闻”扩展作为示例。这个扩展是一个非常特别的(因为它基于 pi 和许多其他原因)并且关于这个扩展的例子的纯粹重复并没有使我更容易理解这个主题。我能找到的其余 10% 的说明,包括官方 Typo3 文档(目前为 here)提供了很好的例子,但没有提到哪个值来自哪里。

我主要对 config.yaml 的顶部部分感兴趣:

  routeEnhancers:  
    {SecondLine}:  
      type: {typedeFinition}  
      extension: {extensionname}  
      plugin: {pluginname}  
      namespace: {namespace}  
      limitToPages:  
        - {a_page_id}  
        - {another_page_id}  
      routes:  
      # routes here...

我需要这些值中的哪些以及我从哪里获取它们?

到目前为止,我发现的说明通常要么根本不解释这些值,要么用“从您的 ext_localconf.PHP获取这些值”来引用它们。这对我没有多大帮助,因为这并没有解释 config.yaml 中的哪个值对应于 ext_localconf.PHP 中的哪个值,以及相应值的语法是否需要小写,首字母大写,其余小写或驼峰,如果它需要在单引号或双引号中,转义,可以包含空格,需要下划线或任何其他语法要求才能有效。诸如“扩展名”之类的术语可能会产生误导,在我的示例中,这可能是“sortitems”、“xyz_sortitems”、“XYZ.sortitems”、“XyzSortitems”或“Sortitems”,仅举几例。

我希望得到一个带有 config.yaml 代码示例的答案,以通用的方式详细解释这些值,以便阅读此问题并遇到相同问题的每个人都能像我一样理解手册可以轻松地将这些宝贵的知识应用到他们自己的自定义 Typo3 扩展中。

解决方法

根据您的问题,这是对值的解释。

routeEnhancers:  
    {SecondLine}:  
      type: {typedefinition}  
      extension: {extensionname}  
      plugin: {pluginname}  
      namespace: {namespace}  
      limitToPages:  
        - {a_page_id}  
        - {another_page_id}  
      routes:  
      # routes here...

{SecondLine} 的值

示例:XzySortitemsShow

增强器的唯一名称,用于内部引用。 Found in core doc

{typedefinition} 的值

示例:Extbase

TYPO3 带有以下开箱即用的路由增强器:

  • 简单增强器(增强器类型“简单”)
  • 插件增强器(增强器类型“插件”)
  • Extbase 插件增强器(增强器类型“Extbase”)

更多类型在 core docs 中描述。

{extensionname} 的值

示例:XyzSortitems

扩展名的大写字母(你的文件夹的名称) 扩展)

来源:typo3_src-10.4.14/sysext/extbase/Classes/Utility/ExtensionUtility.php

configurePlugin() 的函数参数是

public static function configurePlugin(
    $extensionName,$pluginName,array $controllerActions,array $nonCacheableControllerActions = [],$pluginType = self::PLUGIN_TYPE_PLUGIN)

configurePlugin() 代码注释 * @param string $extensionName 扩展名(大写驼峰) 或扩展键(在下划线中)

{pluginname} 的值

示例:Sortitems

来源:typo3_src-10.4.14/sysext/extbase/Classes/Utility/ExtensionUtility.php

configurePlugin() 的函数参数是

public static function configurePlugin(
    $extensionName,$pluginType = self::PLUGIN_TYPE_PLUGIN)

configurePlugin() 代码注释

* @param string $pluginName must be a unique id 
      for your plugin in UpperCamelCase 
      (the string length of the extension key 
      added to the length of the plugin name 
      should be less than 32!)

{命名空间}

是输入 extbase 的替代。

示例可以在 here in core doc 中找到,插件增强器在 here in core doc 中描述。

对于 Extbase Plugin Enhancer,也可以配置 通过跳过扩展和插件属性直接命名空间和 就像在常规插件增强器中一样使用命名空间属性。

离题: Georg Ringer 的 EXT:news 不是基于 pi 的!