首页 > 解决方案 > TYPO3 v9.5 Extbase 错误处理与 routeEnhancers

问题描述

在我的 Extbase TYPO3 扩展中,当记录不再可用(隐藏或删除)时,我想显示一个自定义流体模板。错误处理加载一个流体模板,其中路径在 setup.typoscript 中定义。

但是当我在站点 config.yaml 文件中为我的扩展添加 routEnhancers 时,错误处理不再起作用,它只显示默认的 TYPO3 错误页面:“请求的页面不存在”。

在站点配置的文档中,我没有找到任何方法来为我的分机设置特殊的错误处理。

这是到目前为止处理它的代码:

控制器:

class RecordController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{


    /**
     * Error handling if no entry is found
     *
     * @param string $configuration configuration what will be done
     * @throws \InvalidArgumentException
     * @return string
     */
    protected function handleNoRecordFoundError($configuration)
    {
        $statusCode = HttpUtility::HTTP_STATUS_404;
        HttpUtility::setResponseCode($statusCode);

        $this->getTypoScriptFrontendController()->set_no_cache('Record record not found');

        $standaloneTemplate = GeneralUtility::makeInstance(StandaloneView::class);
        $standaloneTemplate->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($configuration));
        return $standaloneTemplate->render();
    }

    /**
     * @return TypoScriptFrontendController
     */
    protected function getTypoScriptFrontendController()
    {
        return $GLOBALS['TSFE'];
    }
    /**
     * action show
     * @param \Digitalgizmo\Vehicles\Domain\Model\Vehicle $vehicle
     * @return void
     *
     *
     */
    public function showAction(\Vendor\MyExt\Domain\Model\Record $record = null)
    {

        if ($record !== null){

            $this->view->assign('record', $record);
        }
        else {
            $errorContent = $this->handleNoRecordFoundError($this->settings['show']['errorTemplate']);
            if ($errorContent) {
                return $errorContent;
            }
        }


    }
}

配置.yaml;

routeEnhancers:
  MyExt:
    type: Extbase
    extension: MyExt
    plugin: MyExt
    routes:
      -
        routePath: '/staticName/{uid}/{slug}'
        _controller: 'ControllerName::show'
        _arguments:
          slug: record
          uid: id
    defaultController: 'ControllerName::show'
    aspects:
      slug:
        type: PersistedAliasMapper
        tableName: tx_myext_domain_model_record
        routeFieldName: slug
        routeValuePrefix: /
      uid:
        type: PersistedAliasMapper
        tableName: tx_myext_domain_model_record
        routeFieldName: uid

如果记录可用,RoutEnhancer 就可以正常工作。

我怎样才能捕捉到那个错误,所以我可以处理它并显示我的流体模板?我的 showAction 甚至没有被加载(用 XDebug 测试)。我认为这是因为 TYPO3 内核引发了错误。

标签: typo3extbasetypo3-9.x

解决方案


该代码似乎一切正常,问题在于 RouteEnhancer 受到与您的 showAction 相同的约束的影响:一旦删除记录, routeEnhancer 中的 resolve 方法将不再能够找到它或它的 slug。

作为参考,请参阅 API 中的解析函数:https ://api.typo3.org/9.5/_persisted_alias_mapper_8php_source.html 。它实例化了一个 queryBuilder,默认情况下,它构建一个 deleted=0 子句。

要通过他们的 slug 删除 redcords,您需要做的是构建一个自定义 RouteEnhancer,也许通过扩展 PersistendAliasMapper 类以使其也找到已删除记录的方式,请参阅https://docs.typo3.org/m/typo3/ reference-coreapi/master/en-us/ApiOverview/Routing/ExtendingRouting.html,但请注意其中的含义:即使设置了 eval=uniqueInSite 选项,模型中的 slug 字段也将不再能够找到碰撞的 slug,因为也只能看到未删除的记录。


推荐阅读