We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36764 ☆ A M B ☆
    • 45 Posts
    Evengard aka Eiventeleiron Reply #11, 15 years, 4 months ago
    Thank you, that is a good info!

    One question so far - how to get the current relative request path? By using $_SERVER["REQUEST_URI"] or there is a built in modx method?

    UPD: solved. Use $modx->resourceIdentifier.

    UPD2: Publishing my plugin in case if anyone needs it (event - OnPageNotFound):
    <?php
    if($modx->request->getResourceMethod()!="alias")
    	return;
    $uri = $modx->resourceIdentifier;
    $uriChunks = explode("/", $uri);
    $paramNum=0;
    $uriChunksCount = count($uriChunks);
    $paramDelimiter="-"; //DO NOT use "/"! Set the desired delimiter here.
    for($i=$uriChunksCount-1;$i>0;$i--)
    {
    	$parameter=explode($paramDelimiter, $uriChunks[$i], 2);
    	if(count($parameter)!=2)
    		return;
    	$modx->request->parameters['GET'][$parameter[0]]=$parameter[1];
    	if(empty($modx->request->parameters['POST'][$parameter[0]]))
    		$modx->request->parameters['REQUEST'][$parameter[0]]=$parameter[1];
    	$paramNum++;
    	unset($uriChunks[$i]);
    	$uri = implode("/", $uriChunks);
    	if (array_key_exists($uri, $modx->aliasMap))
    	{
    		$modx->sendForward($modx->aliasMap[$uri]);
    	}
    }
    if($paramNum==($uriChunksCount-1))
    {
    	$parameter=explode($paramDelimiter, $uriChunks[0], 2);
    	if(count($parameter)!=2)
    		return;
    	$modx->request->parameters['GET'][$parameter[0]]=$parameter[1];
    	if(empty($modx->request->parameters['POST'][$parameter[0]]))
    		$modx->request->parameters['REQUEST'][$parameter[0]]=$parameter[1];
    	$modx->sendForward($modx->getOption('site_start', null, 1));
    }


    UPD3: updated the code above

    UPD4: added the posibility to change the delimiter

    UPD5: Examples:
    http://example.com/my/lovely-page.html/param1-info/act-dis-play
    will resolve to:
    * page with path my/lovely-page.html (doesn’t break page paths with the param delimiter!)
    * GET param $_GET["param1"]="info"
    * GET param $_GET["act"]="dis-play"

    http://example.com/foo-bar
    will resolve to:
    * Main context (or site) page
    * GET param $_GET["foo"]="bar"

    http://example.com/my/lovely-page.html/param1-foo/bar/param2-alfa
    will lead to page 404 because "bar" won’t be recognised as parameter. Still you will have access to the GET param $_GET["param2"]="alfa" (the parsing is done from right to left)
      • 19328
      • 433 Posts
      Sorry to revive such an old topic... I've always used this code with no problems, but I recently updated a site from Revo 2.2.5 to 2.2.14 and found it no longer works... Trying to figure out what part of the code is giving problems, but can't find it yet. Anyone know if something would have changed in the MODX core between 2.2.5 - 2.2.14 that could be causing this?
        • 19328
        • 433 Posts
        I found something...

        The URL is entered without a trailing slash. For example: www.mysite.nl/page/item-3

        However, in the plugin, the following line:
        4. $uri = $modx->resourceIdentifier;


        Gives: www.mysite.nl/page/item-3/

        With trailing slash. The result is that the last chunk of the URI is empty (it's the part after the trailing slash). This means the line

        12. if(count($parameter)!=2)


        is true (there are not two parameters in the last chunk) and the plugin returns without doing anything.

        When I enter the URL with a trailing slash, line 4 in this plugin in reverse gives the URL without the trailing slash, and everything works. That's strange, isn't it?

        I have not changed the URLs, they always worked without trailing slash. Why it's suddenly not working anymore is a mystery for now.
        I wonder why $modx->resourceIdentifier gives the extra trailing slash if it wasn't there in the first place, and why it is removed when it IS there.

        Any help would be appreciated!