【发布时间】: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