On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi,

    I followed the steps in Mark Hamstra's 'Developing RESTful APIs' (https://docs.modx.com/revolution/2.x/developing-in-modx/advanced-development/developing-rest-servers).

    The first step (Bootstrapping the API) seems to be OK. An undefined endpoint results in the false "success message" as indicated in the document.

    But whenever I try to build an API endpoint, I get a 502 Bad Gateway error. Even with the simple "modResource" classKey property.

    The nginx error log entry looks like this:

    2019/03/02 08:03:04 [error] 17072#17072: *62732 upstream sent invalid status "-1 Internal Server Error" while reading response header from upstream, client: 81.164.224.235, server: domain.com, request: "GET /conversatie/rest/items HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "domain.com"

    I ran it on a "fresh" Modx (2.7.1.).

    Any clues?

    Ludo


    • Can you post what your end point looks like?
        ■ email: lk@alienbuild.uk | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
      • Quote from: lkfranklin at Mar 04, 2019, 03:48 PM
        Can you post what your end point looks like?

        Just this one::

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

        under the name Items.php

        Ludo
        • Can you check your PHP and MODX error logs as well? That hopefully gives a better error message. The nginx one basically just says PHP didn't return what it expected.

          Could be an issue where the name of your controller does not match the required/configured naming pattern.
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
          • MODX is very picky about the class name of a controller, and not very graceful when you get it wrong.

            IIRC, the format is: modNamespaceAction...Controller

            (The part where the dots are is optional -- it's often "Manager" for CMPs.)

            You should also return the controller class name at the end of the class file (outside of the class):

            return 'LoginConfirmRegisterController';



              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
            • Thank you Mark for taking the trouble to respond.

              I've managed to get some feedback from the MODX error log.

              It now tells me:

              Could not get table name for class: Quotation
              


              Hence I presume that this part of my code (in index.php) has been handled correctly:

              $path = $modx->getOption('quotes.core_path', null, 
              	   $modx->getOption('core_path').'components/quotes/') . 'model/quotes/';
              $modx->getService('quotation', 'quotation', $path);
              


              The php-log doesn't tell me anything. Which might tell you something.

              About the name of the controller, see my response to Bob.


              Quote from: markh at Mar 17, 2019, 02:45 PM
              Can you check your PHP and MODX error logs as well? That hopefully gives a better error message. The nginx one basically just says PHP didn't return what it expected.

              Could be an issue where the name of your controller does not match the required/configured naming pattern.


              Ludo
              • Thanks, Bob, for your explanation.

                I'm afraid it led to some confusion as the example in the main documentation

                class MyControllerItems extends modRestController {
                    public $classKey = 'ToDoItem';
                    public $defaultSortField = 'sortorder';
                    public $defaultSortDirection = 'ASC';
                }
                


                seems to be incompatible with the format you specifiy. The classname 'MyControllerItems' does not contain 'mod' for instance.

                By following your instructions on creating a custom (Quotation) DB, I created the file 'quotation.class.php' under 'components/quotes/model/quotes'. I tried to use that classname, but to no avail. Could you explain the classname to use for that quotes table?

                Kind regards

                Ludo




                Quote from: BobRay at Mar 18, 2019, 02:07 AM
                MODX is very picky about the class name of a controller, and not very graceful when you get it wrong.

                IIRC, the format is: modNamespaceAction...Controller

                (The part where the dots are is optional -- it's often "Manager" for CMPs.)

                You should also return the controller class name at the end of the class file (outside of the class):

                return 'LoginConfirmRegisterController';



                • Sorry if I confused you. The 'mod' is not necessary. In my example, you'd replace 'modNameSpace' with the actual namespace name. You'd also put the word "Controller" at the end, not the beginning.

                  If you don't have a Menu item in the Manager to call your controller, you may not need the "action" part, but "Controller" may still need to be at the end.

                  I'd also recommend putting your controller in the core/components/mynamespace/controllers/ directory.

                  When you load a controller with the path: core/components/mynamespace/controllers/mycontroller.class.php, MODX expects to find a class inside that's named MynamespaceMycontrollerController. If it doesn't, it thinks it has the wrong file and bails out. The first part is the actual name of the namespace. The second is the name of the action, but should match the first part of the filename except for case (filename in lowercase). The third part is just the word "Controller."

                  The Quotations class isn't really relevant because there's no controller -- it's just a standard HTML form with a snippet.

                  All that said, your controller file may contain a syntax error that's causing the server error. In that case correcting the controller name won't fix things. A good code editor like PhpStorm is worth its weight in gold for flagging syntax errors that might otherwise take hours to track down, especially since PHP 7 likes to throw a server error instead of a specific PHP error when it finds bad syntax.



                  [ed. note: BobRay last edited this post 6 months, 3 weeks ago.]
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting