【问题标题】:RealURL: Remove Controller and Action from URLRealURL:从 URL 中删除控制器和操作
【发布时间】:2014-10-01 13:30:29
【问题描述】:

我有一个带有列表和显示操作的扩展程序。目前这个扩展可以出现在多个页面上:

/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']);
}

现在我得到如下 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。

【问题讨论】:

  • 它需要另一种方法,是你的扩展吗?你能改变它的代码吗?告诉我你如何建立你的链接/网址
  • @biesior 是的,这是我自己的扩展,是的,我可以改变一切。链接是这样构建的:<f:link.action action="show" arguments="{article : article}" class="more" title="{article.name}">show article</f:link.action>

标签: typo3 extbase realurl


【解决方案1】:

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

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

它会生成像

这样的 url
/current/page/?article=123

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

接下来在插件的第一个操作(可能是list)中,如果给定的参数存在,您只需将请求转发到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 - 就像一个魅力。非常感谢。我稍微更改了代码以使其更加通用。我将签名更改为:public function showAction(\namespace $article = null) 并将新添加的部分包装到此:if (!$article) { … }
  • 我在我的扩展程序中使用了@transient 属性(请参阅stackoverflow.com/questions/27274477/…) - 是否使用上述方法会失败?当我让它工作时,所有那些“瞬态”数据都不再输出了
  • 在使用“action”链接时,似乎 extbase 对属性类型(我设置错误)的严格程度比现在使用替代过程的要低。它与“瞬态”无关
  • 该解决方案非常有效。非常感谢,在某些情况下,使用标准的 get 变量可能会更好,例如 tx_yourext['article']
  • 这个答案在很多方面违反了领域驱动设计和关注点分离的方面。每个动作都应使用一致且有效的参数集直接调用 - 这是请求处理程序的工作,而不是任何控制器逻辑的工作。绕过请求/响应分离并直接获取 GET 参数是另一种违规行为 - 在处理未经验证的用户提交的数据(SQLi、XSS 等)方面是危险的。即使它有效,它仍然是 hacky...
【解决方案2】:

如果使用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 绕过结合使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    相关资源
    最近更新 更多