Created
July 11, 2017 08:54
-
-
Save einpraegsam/b8b2ebc7a9473f74033b8e81d0cdb310 to your computer and use it in GitHub Desktop.
Individual URI-path with realurl in TYPO3. In the following example we need to have paths like /lastname.firstname for a detailview on contacts. And of course we don't want to have redirects. Thinking of a configuration in realurl that uses /folder1/folder2/Action/lastname.firstname that will be extended with the classes below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!defined('TYPO3_MODE')) { | |
die('Access denied.'); | |
} | |
call_user_func(function () { | |
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['encodeSpURL_postProc'][] = | |
'EXT:extkey/Classes/Hooks/RealurlEncoding.php:In2code\Extkey\Hooks\RealurlEncoding->convert'; | |
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['decodeSpURL_preProc'][] = | |
'EXT:extkey/Classes/Hooks/RealurlDecoding.php:In2code\Extkey\Hooks\RealurlDecoding->convert'; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace In2code\Extkey\Hooks; | |
/** | |
* Class RealurlDecoding | |
*/ | |
class RealurlDecoding | |
{ | |
/** | |
* @param array $parameter | |
* @return void | |
*/ | |
public function convert(array &$parameter) | |
{ | |
if ($this->isDetailLink($parameter)) { | |
$newUrl = $this->getSpeakingUrlFromWebShortage($parameter['URL']); | |
if ($newUrl !== '') { | |
$parameter['URL'] = $newUrl; | |
} | |
} | |
} | |
/** | |
* Check if uri is just something like "lastname.firstname" | |
* | |
* @param array $parameter | |
* @return bool | |
*/ | |
protected function isDetailLink(array $parameter): bool | |
{ | |
return preg_replace('~([a-z\-]+\.[a-z\-]+)~', '', $parameter['URL']) === ''; | |
} | |
/** | |
* Get speaking url from database from webshortage + prepending "/" | |
* @param string $webShortage | |
* @return string | |
*/ | |
protected function getSpeakingUrlFromWebShortage(string $webShortage): string | |
{ | |
$row = (array)$GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow( | |
'speaking_url', | |
'tx_realurl_urldata', | |
'speaking_url like "%' . $webShortage . '%" and (expire = 0 or expire > ' . time() . ')' | |
); | |
if (!empty($row['speaking_url'])) { | |
return '/' . $row['speaking_url']; | |
} | |
return ''; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace In2code\Extkey\Hooks; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
/** | |
* Class RealurlEncoding | |
*/ | |
class RealurlEncoding | |
{ | |
/** | |
* @param array $parameter | |
* @return void | |
*/ | |
public function convert(array &$parameter) | |
{ | |
if ($this->isDetailLink($parameter)) { | |
$parameter['URL'] = $this->getWebShortageFromUri($parameter['URL']); | |
} | |
} | |
/** | |
* Check if there is the need of building a link with &tx_extkey_pi1[action]=detail | |
* | |
* @param array $parameter | |
* @return bool | |
*/ | |
protected function isDetailLink(array $parameter): bool | |
{ | |
return !empty($parameter['params']['args']['addParams']) | |
&& stristr($parameter['params']['args']['addParams'], '&tx_extkey_pi1[action]=detail'); | |
} | |
/** | |
* Get last folder from path | |
* "/folder1/folder2/lastname.firstname/" => "lastname.firstname" | |
* | |
* @param string $uri | |
* @return string | |
*/ | |
protected function getWebShortageFromUri(string $uri): string | |
{ | |
$parts = GeneralUtility::trimExplode('/', $uri, true); | |
return end($parts); | |
} | |
} |
Yes, you're right. Something like https://gist.github.com/einpraegsam/5214a4eb3b9ef6b932fbf92d43400986#file-selectexample1-php instead of using $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(). This will not be possible in TYPO3 9 any more.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bonus if you use the querybuilder ;-) but this is nice. Better than modify realurl_conf