We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37984
    • 215 Posts
    I'm somewhat new to the backend development aspect of MODX, and I've managed to implement my endpoint successfully using the information at https://gist.github.com/splittingred/2346752. I created a custom database table via the comments that "Pixelchutes" made on that page, and I've successfully tested GET, POST, PUT & DELETE using Postman.

    The issue I'm running into is that I need to be able to customize the schema. My current schema was written in XML when I ran the CreateXpdoClasses snippet as part of BobRay's Custom Database Table tutorial here: http://bobsguides.com/custom-db-tables.html

    The schema provided by my client looks like this:

    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "title": "Publish Item",
        "description": "",
        "type": "object",
        "properties": {
            "data": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                          "id" : {
                              "type": "string"
                          }, 
                          "title" : {
                              "type": "string"
                          },
                          "description" : {
                              "type": "string"
                          },
                          "required": ["id", "title", "description"]
    	}
                },
                "minItems": 1,
                "uniqueItems": true
            }
        },
        "required": ["data"]
    }
    


    and a POST request to that schema would look like this:

    { "data" : [
        {
        "id" : "9140555314413871701", 
        "title": "Name",
        "description" : "<b>Item Description</b>"
     },
        {
        "id" : "9140555314413871805", 
        "title": "Name 2",
        "description" : "<b>Item Description 2</b>"
      }
        ]
    }
    


    Can anyone point me in the right direction here? [ed. note: designcouch last edited this post 10 years, 9 months ago.]
      Jesse Couch
      MODX Aficionado, Front-End Designer & Developer
      http://www.designcouch.com
      • 11055 ☆ A M B ☆
      • 3,112 Posts
      You need to override the class that returns the output.
      We do this when integrating MODX-Rest with dojotoolkit.org
      We even override all the CRUD methods for it.

      I don't know how you set the redirecting up, but this is an example of what we do for the class:

      I'm taking the example: Box.php

      <?php
      
      /**
       * Found at: Controllers/Box.php
       *
       * Handle requests to [URL]/Controllers/Box. Automagically handles CRUD (GET/POST/PUT/DELETE) for the xPDOObject class myBox.
       */
      class myControllersBox extends modRestController {
      
          public $classKey = 'myBox';
          public $defaultSortField = 'name';
          public $defaultSortDirection = 'DESC';
      
          /**
           * Handle DELETE requests
           * @return array
           */
          public function delete() {
              // override the delete action in here...
      
              // otherwise...
              return parent::delete();
          }
      
          /**
           * Handles updating of objects
           * @return array
           */
          public function put() {
              // override the put action in here...
      
              // otherwise...
              return parent::put();
          }
      
          /**
           * Method for routing POST requests. Can be overridden; default behavior automates xPDOObject, class-based requests.
           * @abstract
           */
          public function post() {
              // override the post action in here...
      
              // otherwise...
              return parent::post();
          }
      
          /**
           * Abstract method for routing GET requests without a primary key passed. Must be defined in your derivative
           * controller. Handles fetching of collections of objects.
           *
           * @abstract
           * @return array
           */
          public function getList() {
              // override the getList action in here...
      
              // otherwise...
              return parent::getList();
          }
      
          /**
           * Output a collection of objects as a list.
           *
           * @param array $list
           * @param int|boolean $total
           * @param int $status
           */
          public function collection($list = array(), $total = false, $status = null) {
              // override the collection output fron getList action above in here...
      
              // otherwise...
              return parent::collection($list, $total, $status);
          }
      
          /**
           * Abstract method for routing GET requests with a primary key passed. Must be defined in your derivative
           * controller.
           * @abstract
           * @param $id
           */
          public function read($id) {
              // override the read action in here...
      
              // otherwise...
              return parent::read($id);
          }
      }
      


      Since these methods are the basic actions, we usually create a parent class to reduce duplication.

      myParentController.php
      <?php
      class myParentController extends modRestController {
          // all the overriding classes above are here...
      }
      


      Box.php
      <?php
      /**
       * Found at: Controllers/Box.php
       *
       * Handle requests to [URL]/Controllers/Box. Automagically handles CRUD (GET/POST/PUT/DELETE) for the xPDOObject class myBox.
       */
      
      include_once 'path/to/myParentController.php';
      
      class myControllersBox extends myParentController {
          // define the classKey in here
      
          public $classKey = 'myBox';
          public $defaultSortField = 'name';
          public $defaultSortDirection = 'DESC';
      
          // nothing in here, except you have specific overriding algorithm for the particular $classKey for any particular method
      }
      


      I hope you get the idea.
        Rico
        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
        MODx is great, but knowing how to use it well makes it perfect!

        www.virtudraft.com

        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

        Maintainter/contributor of Babel

        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
        • 37984
        • 215 Posts
        Awesome response, @goldsky. I imagined that it would have something to do with the controller, but as I mentioned in my initial post, REST in MODX is fairly new to me. Going to take a shot at this today and I'll let everyone know how it goes.
          Jesse Couch
          MODX Aficionado, Front-End Designer & Developer
          http://www.designcouch.com