Generating dynamic friendly urls with CustomRequest

Hey guys,

I’m currently developing a vehicle car site on modx cloud and I’m using an API to get the vehicle details. I have a resource (27) that uses the GET vehicleid to pull information about that particular model.

I want to generate a friendly url that converts

myexample .com/vehicles?vehicleid=xxxxx&make=mercedes-benz&model=a-sedan

to

myexample .com/vehicles/mercedes-benz/a-sedan/xxxxx

I’m seeing that CustomRequest can achieve this but I have tried multiple combinations and nothing seems to work.

Here is my setup.

Server: Ngnix (modxcloud)

In my custom request config
Configuration Name: Custom URI
Alias Path: make/model/vehicleid
Resource: Vehicles (27)
URI Parameter: [“make”, “model”, “vehicleid”]
Regular Expression: Blank

My Chunk

    <a href="[[!mycustomurl?
        	    &id=`27`
        	    &make=`[[+dmsmake]]`
        	    &model=`[[+dmsmodel]]`
        	    &vehicleid=`[[+itemid]]`
	         ]]" 
        title="[[+dmsmake]] [[+dmsmodel]] [[+dmsgrade]]">
	</a>

My Snippet

<?php
$id = (int) $modx->getOption('id', $scriptProperties);
$make = $modx->getOption('make', $scriptProperties);
$model = $modx->getOption('model', $scriptProperties);
$vehicleid = $modx->getOption('vehicleid', $scriptProperties);


$sfl = $modx->getService('sfl_countries', 'sfl_countries', $modx->getOption('core_path') . 'components/sfl_countries/' . 'model/sfl_countries/');
if (!($sfl instanceof sfl_countries))
    return '';

if ($make == "Mercedes"){
    $make = "Mercedes Benz";
}

$make = $sfl->slugify($make);
$model = $sfl->slugify($model);

$url = $modx->makeUrl($id, '', array(
    'make' => $make,
    'model' => $model,
    'vehicleid' => $vehicleid
));

return $url;

Can anyone point out what I am missing?

I suppose your Alias Path is wrong. It should be left blank or just say vehicles/.

Hey, thanks for the reply. I just tried your suggestion and it didn’t work.

Maybe it is just a misunderstanding of what the Extra CustomRequest really does. I think it only parses a request and doesn’t help in generating an url at all. You probably have to append the parameters yourself.

$url = $modx->makeUrl($id, '', '', 'full');
$url .= '/' . $make . '/' . $model . '/' . $vehicleid;
1 Like

For this FURL to work, the location of the page in the Resource tree would have to be:

web
    vehicles
       mercedes-benz
          a-sedan/
              xxxxx*

If I’m understanding you, (and I’m probably not), you would have to, in your snippet, place the resource there by creating the necessary directories (if they don’t exist already) and setting all the parents.

@bobray CustomRequest does this routing, if it is configured right. No need to create those resources.

@halftrainedharry is right with the url generation. The snippet can’t create the url with makeUrl.

@random_noob The aliaspath has to be empty in the config.

Thanks @jako, I suspected that I had it wrong.

I see and understand now. Thank you. It is working now.

Ok I understand now. Thanks for the assist.