RealURL:从URL中删除Controller和Action

时间:2014-10-01 13:30:29

标签: typo3 extbase realurl

我有一个包含列表和展示操作的扩展程序。目前,此扩展程序可以显示在多个页面上:

/page-1/
/page-2/subpage/

我已经配置了realurl

$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']=array (
    'encodeSpURL_postProc' => array('user_encodeSpURL_postProc'),
    'decodeSpURL_preProc' => array('user_decodeSpURL_preProc'),
    '_DEFAULT' => array (
        …
        'postVarSets' => array(
            '_DEFAULT' => array(
                'controller' => array(
                    array(
                        'GETvar' => 'tx_extension_plugin[controller]',
                        'noMatch' => 'bypass',
                    ),
                ),
                'extension' => array(
                    array(
                        'GETvar' => 'tx_extension_plugin[action]',
                    ),
                    array(
                        'GETvar' => 'tx_extension_plugin[controller]',
                    ),
                    array(
                        'GETvar' => 'tx_extension_plugin[value]',
                        'lookUpTable' => array(
                            'table' => 'table',
                            'id_field' => 'uid',
                            'alias_field' => 'name',
                            'addWhereClause' => ' AND NOT deleted AND NOT hidden',
                            …
);

function user_decodeSpURL_preProc(&$params, &$ref) {
    $params['URL'] = str_replace('page-1/', 'page-1/extension/', $params['URL']);
}

function user_encodeSpURL_postProc(&$params, &$ref) {
    $params['URL'] = str_replace('page-1/extension/', 'page-1/', $params['URL']);
}

现在我得到的网址如下:

/page-1/ /* shows list */
/page-1/Action/show/name-of-single-element /* single view */

我真正想要的是:

/page-1/name-of-single-element /* single view */

如何摆脱动作和控制器?

如果我删除:

array('GETvar' => 'tx_extension_plugin[action]'),
array('GETvar' => 'tx_extension_plugin[controller]'),

它将参数附加到URL。

2 个答案:

答案 0 :(得分:7)

使用f:link.action VH时,您无法添加所有内容,而是需要使用f:link.page并仅传递必需的参数,示例:

<f:link.page additionalParams="{article : article.uid}" class="more" title="{article.name}">show article</f:link.page>

它会生成类似

的网址
/current/page/?article=123

/current/page/we-added-realurl-support-for-article

插件的第一个操作中的下一个(可能是list),如果存在param,则只需将请求转发给show操作:

public function listAction() {
    if (intval(\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('article'))>0) $this->forward('show');

    // Rest of code for list action...
}

可能会更改show

的签名
public function showAction() {

    $article = $this->articleRepository->findByUid(intval(\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('article')));

    if ($article == null) {
        $this->redirectToUri($this->uriBuilder->reset()->setTargetPageUid($GLOBALS['TSFE']->id)->build());
    }


    // Rest of code for show action...
}

答案 1 :(得分:6)

如果使用URIbuilder,您还可以使用配置:

features.skipDefaultArguments = 1
例如

;

# if enabled, default controller and/or action is skipped when creating URIs through the URI Builder 
plugin.tx_extension.features.skipDefaultArguments = 1

我将此配置与realurl bypass一起使用

'postVarSets' => array(
  '_DEFAULT' => array(
    'extbaseParameters' => array(
      array(
        'GETvar' => 'tx_extension_plugin[action]',
        'noMatch' => 'bypass',
      ),
    ),
  ),
),