【问题标题】:Typo3 custom extension route enhancersTypo3 自定义扩展路由增强器
【发布时间】:2021-05-07 08:24:28
【问题描述】:

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

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

一个item具有itemid、name、date和amount属性。

我的 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,sortbydate,sortbyamount,showsingleitem']  
        );  
  
    }  
);

我的视图控制器位于typo3conf/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 中使用 Route Enhancer,从 Typo3 数据库中的扩展表中读取 itemid 和名称,并将这些值放入指向单项页面。

不幸的是,我可以找到大约 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 扩展中。

【问题讨论】:

  • 请注意,以上代码大部分是伪代码,以使示例尽可能通用,同时保留最详细的变量名称以避免错误的关键字,如“控制器”或“扩展” .

标签: routes typo3 typo3-9.x typo3-extensions


【解决方案1】:

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

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

扩展名的UpperCamelCase(您的文件夹的名称 扩展名)

来源: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 字符串 $extensionName 扩展名(UpperCamelCase) 或扩展键(在 lower_underscore 中)

{pluginname} 的值

示例:排序项

来源: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 $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 插件增强器,也可以配置 通过跳过扩展和插件属性直接命名空间和 就像在常规插件增强器中一样使用命名空间属性。

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

【讨论】:

  • 非常感谢您提供如此详细而清晰的信息!关键是我直接写到typo3conf/sites/{sitename}/config.yaml 而不是typo3conf/ext/xyz_sortitems/config.yaml。
  • 或者你可以在typo3conf/sites/{sitename}/config.yaml 中导入它:imports: - resource: 'EXT:xyz_sortitems/Configuration/Routes/Default.yaml'
猜你喜欢
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
相关资源
最近更新 更多