Passing a snippet to the REST API

if i had a snippet
<?php return 'Hello, World!'; ?>

how would I pass that into the REST API so that I could output it and consume it in the headless CMS?

I was looking at https://docs.modx.com/3.x/en/extending-modx/modx-class/reference/modx.runsnippet

Does that have any relevance. If anyone could point me in the right direction on what i need to do I will try to figure out the next steps. Thanks!

I’m not quite sure what you are trying to do, but you probably have to override the get() method (or maybe getlist() or read()) of the class modRestController.
As you can put all your code in those functions, there is no need to even use a snippet.

this is what i have so far

<?php
class MyControllerHello extends modRestController {
public function beforePost() { return false; }
public function beforePut() { return false; }
public function beforeDelete() { return false; }

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

public function prepareListObject(xPDOObject $object) {

$data = array();
$data['id'] = $object->id;
$data['pagetitle'] = $object->pagetitle;
$data['content'] = $object->content;
$data['introtext'] = $object->introtext;
$data['longtitle'] = $object->longtitle;
$data['description'] = $object->description;
$data['snippet'] = $object->$modx->runSnippet('hello');

return $data;
}
}

also the $data[‘snippet’] line fails and 500’s so thats what I was wondering was something I could do?

I guess I am starting small and just trying to get that to work but what I’d love to do future tense but didn’t wanna get that deep till I understood a bit more was, i wanted to just have a input for image, that would process by a snippet that would run something like phpthumb and give me back a URL of the image after its been manipulated, that way I can do the processing from the MODX side and the return JSON would contain the compiled / processed image at whatever dimentions i’d setup in the snippet.

am i not on the right track for that type of thing or can you think of a better way to get a processed phpthumb image into a string into a json key :slight_smile:

Use $this->modx->runSnippet(...)

1 Like

sweet brother! that works! thanks! Do you think I am on the right track with what i explained? get a snippet going and pass the data? appreciate your help!

Yes, that sounds reasonable to me. You could call a snippet like pThumb.

$thumb = $this->modx->runSnippet('pthumb', [
    'input' => $my_img,
    'options' => '&w=320&h=200&zc=1'
]);
1 Like

ok thanks for the head start, i think this is the only thing stopping me from being a headless CMS master. :slight_smile: thanks boss

tried it and it works great!

<?php
class MyControllerHello extends modRestController {
public function beforePost() { return false; }
public function beforePut() { return false; }
public function beforeDelete() { return false; }

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


public function prepareListObject(xPDOObject $object) {

    $data = array();
    $data['id'] = $object->id;
    $data['pagetitle'] = $object->pagetitle;
    $data['content'] = $object->content;
    $data['introtext'] = $object->introtext;
    $data['longtitle'] = $object->longtitle;
    $data['description'] = $object->description;
    // $data['h1'] = $this->modx->getChunk('hello');

    $data['thumb'] = $this->modx->runSnippet('pthumb', [
      'input' => $object->getTVValue('imageTV'),
      'options' => '&w=320&h=200&zc=1'
    ]);

    return $data;
  }
}