Building a TYPO3 extension in Extbase/Fluid

Speaking URLs for your plugins with Site Handling and RouteEnhancers (T3v9 and later)

Adding routeEnhancers for extension plugins is surprisingly well documented on the internet. It is part of the Site Handling introduced in T3v9 and lives in the site configuration file /typo3conf/sites/yourSiteIdentifier/config.yaml. So just search for “typo3 routeenhancers” or “typo3 routing enhancers” and you’ll surely find what you need.

Speaking URLs for your plugins using the RealURL extension (T3 before v9)

Basic RealURL configuration

TYPO3 Backend ››› Extension Manager ››› RealURL ››› Configure:

  • set “path to configuration file”, e.g. “typo3conf/realurl_conf.php” (“Enable automatic configuration” should be checked)
  • set “Automatic configuration file format” to “PHP source”
  • delete /typo3conf/realurl_autoconf.php (or, for security reasons, change the filename) then reload the page (frontend) so that this file is being rewritten
  • now copy the newly written file and rename it to realurl_conf.php which you can now edit
    NOTE: the above mentioned steps are also a good way to have the realurl auto-configuration rewritten after installing a 3rd party extension. E.g. the RealURL configuration for the extension “cal” is added to autoconf only after having TYPO3 rewrite it

Configuring your own plugin

add the following configuration to the array

$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['postVarSets']['_DEFAULT'] in the above defined config file typo3conf/realurl_conf.php

'your_extension' => array(
        array(
                'GETvar' => 'tx_yourext_yourplugin[controller]',
                'valueMap' => array(
                        'new-url-name' => 'YourController' // tx_yourext_yourplugin[controller]=YourController changed to new-url-name/
                ),
                'noMatch' => 'bypass',
        ),
        array(
                'GETvar' => 'tx_yourext_yourplugin[action]',
                'valueMap' => array(
                        'detail' => 'show' // tx_yourext_yourplugin[action]=show changed to detail/
                ),
                'noMatch' => 'bypass',
        ),
        array(
                'GETvar' => 'tx_yourext_yourplugin[yourmodel]',
                'lookUpTable' => array(
                        'table' => 'tx_yourext_domain_model_yourmodel',
                        'id_field' => 'uid',
                        'alias_field' => 'property_1',
                        // use concat if you want two properties combined in the URL:
                        // 'alias_field' => 'concat(property_1, \' \', property_2)',
                        'addWhereClause' => ' AND NOT deleted',
                        'useUniqueCache' => 1,
                        'useUniqueCache_conf' => array(
                                'spaceCharacter' => '-',
                                'strtolower' => 1,
                        ),
                ),
        ),
),
        ),
),

NOTE: the array key “your_extension” will be displayed in the URL as are the renamed controller new-url-name and action display. The URL will be

/the-page-that-contains-the-plugin/your_extension/new-url-name/display/property_1-property_2/
In order to remove the unnecessary parts your_extension/new-url-name/display/ follow these steps:

Additional configuration

Remove unnecessary parts from your URL
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'] = array(
        'encodeSpURL_postProc' => array('user_encodeSpURL_postProc'),
        'decodeSpURL_preProc' => array('user_decodeSpURL_preProc'),
        //…
        //…

add these functions called in the config array to the end of the file realurl_conf.php ›››

function user_decodeSpURL_preProc(&$params, &$ref) {
        // replacing some url parts before displaying a page
        $params['URL'] = preg_replace(
                '~the-page-that-contains-the-plugin\/([a-z\-_]+/)~',
                'the-page-that-contains-the-plugin/your_extension/new-url-name/display/$1',
                $params['URL']);
                // needs to be regex, because ‘the-page-that-contains-the-plugin’ is the name of the page;
                // thus TYPO3 would try to find the replacement when the page is called without further GET vars
}
function user_encodeSpURL_postProc(&$params, &$ref) {
        // replacing some url parts before setting a link
        $params['URL'] = str_replace('the-page-that-contains-the-plugin/your_extension/new-url-name/display/', 'the-page-that-contains-the-plugin/', $params['URL']);
}
        $params['URL'] = str_replace('the-page-that-contains-the-plugin/your_extension/new-url-name/display/', 'the-page-that-contains-the-plugin/', $params['URL']);
}

NOTE: Replace “the-page-that-contains-the-plugin” with the name of the page that shows your plugin. Regrettably it’s not possible to replace the parts you want to remove by an empty string or just a slash. So you will need to do it for each page that contains the plugin and after every change.

Problems with cHash? See my notes on Problems with RealURL and cHash