Rest API - makeUrl

A bit stuck here. Call to getObject works fine, but makeUrl or runSnippet does not. What am I missing?
Thanks for any help.

class MyControllerLocations extends modRestController {
   $page = $this->modx->getObject('modTemplateVarResource', array('tmplvarid' => 75, 'contentid' => $id));
   $url = $this->modx->makeUrl(242);
}

https://forums.modx.com/thread/80637/why-does-modx--makeurl-not-fully-work-in-conjunction-with-ondocformsave#dis-post-549370

Maybe this helps?

I’ve tried the full version as well with no luck.
makeUrl(242, “”, “”, “full”);
It is as if I don’t have access to the modx classes. I’ve tried using runSnippet as well with no luck.

Not sure if it helps exactly but I just had a quick go and got it working with this using modResource as the solution.

<?php

class MyControllerResources extends modRestController {

    public $classKey = 'modResource';
    public $defaultSortField = 'id';
    public $defaultSortDirection = 'ASC';

    public function prepareListObject(xPDOObject $object){
    	$objectArray = $object->toArray();
    	$objectArray['fullUrl'] = $this->modx->makeUrl($objectArray['id'], "", "", "full");;
    	return $objectArray;
    }

}

return 'MyControllerResources';

Are you getting any errors, or what are you seeing?

One error I am consistently getting is

[2019-04-30 00:37:59] (ERROR @ /home/site/public_html/cms/core/xpdo/xpdo.class.php : 503) Path specified for package modResource is not a valid or accessible directory: /home/site/public_html/cms/core/model/modx/modresource.class.php

Checked permissions on this file and it is 0644 with directories to file being 0755 as expected.

with $url = $this->modx->makeUrl(25,’’,’’,‘https’); added in I just get a 500 error at the endpoint

class MyControllerLocations extends modRestController {
    public $classKey = 'modResource';
    public $defaultSortField = 'id';
    public $defaultSortDirection = 'ASC';


    public function read($id) {
        if (empty($id)) {
            return $this->failure($this->modx->lexicon('rest.err_field_ns',array(
                'field' => $this->primaryKeyField,
            )));
        }
        /** @var xPDOObject $object */
        $c = $this->getPrimaryKeyCriteria($id);
        $this->object = $this->modx->getObject($this->classKey,$c);

        if (empty($this->object)) {
            return $this->failure($this->modx->lexicon('rest.err_obj_nf',array(
                'class_key' => $this->classKey,
            )));
        }

        $locations = $this->modx->getObject('modTemplateVarResource', array('tmplvarid' => 75, 'contentid' => $id));
        $locationsValue = json_decode($locations->get('value'));
        
        $internationalLocations = $this->modx->getObject('modTemplateVarResource', array('tmplvarid' => 76, 'contentid' => $id));
        $internationalLocationsValue = json_decode($internationalLocations->get('value'));


        $objectArray = Array();
        if ($locations) $objectArray['locations'] = $locationsValue;
        if ($internationalLocations) $objectArray['internationalLocations'] = $internationalLocationsValue;
        
        //test
        //$link = "[[~357]] blah [[~242]]";
        //$matches = array();
        //$t = preg_match("'\[\[\~(.*?)\]\]'si", $link, $matches);
        //$url = $this->modx->makeUrl(25,'','','https');
        //$objectArray['test'] = $url;


        $afterRead = $this->afterRead($objectArray);
        if ($afterRead !== true && $afterRead !== null) {
            return $this->failure($afterRead === false ? $this->errorMessage : $afterRead);
        }

        return $this->success('',$objectArray);
    }

}

From what I can tell you’re just trying to pull data from a TV and include it in the end point? If so then this should work for you. Just replace ‘tvname’ with the name of the TV you want to pull the data from.

<?php

class MyControllerResources extends modRestController {

    public $classKey = 'modResource';
    public $defaultSortField = 'id';
    public $defaultSortDirection = 'ASC';

    public function prepareListObject(xPDOObject $object){
    	$objectArray = $object->toArray();
    	$objectArray['fullUrl'] = $this->modx->makeUrl($objectArray['id'], "", "", "full");
    	$objectArray['locations'] = $this->object->getTVValue('tvname'); 
    	$objectArray['internationalLocations'] = $this->object->getTVValue('tvname'); 
    	return $objectArray;
    }

}

return 'MyControllerResources';

You’re getting a 500 internal server error, so you need to look at your server error log for details.

Perhaps $this->modx isn’t a modX instance but an xPDO instance? That could explain why getObject works but not makeUrl. Perhaps the problem is that you need to first initialize/load a context before makeUrl can get to the context cache? The error in the server/php error log will hopefully provide answers, so we don’t have to guess.

This suggests you’re trying to load a package modResource, which indeed doesn’t exist. That might be coming from your index.php where you call $modx->addPackage()? The package for modResource is modx, and already automatically loaded when MODX is initialised. Could be useful if you also post your index file.

[30-Apr-2019 00:47:17 UTC] PHP Fatal error:  Uncaught Error: Call to a member function makeUrl() on null in /home/site/public_html/cms/core/model/modx/modx.class.php:967
Stack trace:
#0 /home/site/public_html/cms/rest/Controllers/locations.php(62): modX->makeUrl(25, '', '', 'https')
#1 /home/site/public_html/cms/core/model/modx/rest/modrestcontroller.class.php(437): MyControllerLocations->read('27')
#2 [internal function]: modRestController->get()
#3 /home/site/public_html/cms/core/model/modx/rest/modrestservice.class.php(158): ReflectionMethod->invoke(Object(MyControllerLocations))
#4 /home/site/public_html/cms/rest/index.php(23): modRestService->process()
#5 {main}
  thrown in /home/site/public_html/cms/core/model/modx/modx.class.php on line 967