phptypo3extbaserealurl

RealURL: Remove Controller and Action from URL


I have an extension with a list and show action. Currently this extension can appear on multiple pages:

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

I have configured realurl like that:

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

Now I get URLs like:

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

What I actually want is this:

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

How do I get rid of the action and controller?

If I remove:

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

it appends the parameters to the URL.


Solution

  • You can't avoid adding all the stuff when using f:link.action VH, instead you need to use f:link.page and pass only required params, sample:

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

    it will generate url like

    /current/page/?article=123
    

    or

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

    next in your first action of plugin (probably list) you just need to forward request to show action if given param exists:

    public function listAction() {
        if (intval(\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('article'))>0) $this->forward('show');
    
        // Rest of code for list action...
    }
    

    and probably change signature of 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...
    }