We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18809
    • 109 Posts
    Hello Modxers
    I want to build android apps with Modx back end to get data from my Modx sites in app.
    I did this in Wordpress with REST api plugin.

    I want to move all my wordpress sotes to Modx and I am planning to make Android apps for those sites.

    So basically I want to fetch data from my Modx sites's pages to app.

    For example I have a blog on my Modx site using BlogIt extra and I want to get posts from specific tags to android app then how to get data from posts include images and chunks??
    I want to learn this game changer(for me) method. I also use ionic.

    If anyone know anything about this then please help me to get this done.
    Thanks smiley
      My Proudly Powerd By Modx site - http://irresistiblemt.in smiley Its simple site but its made in best CMS.
      • 3749
      • 24,544 Posts
        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
        • 18809
        • 109 Posts
        Quote from: BobRay at Oct 07, 2017, 03:09 AM
        This may help:


        https://docs.modx.com/revolution/2.x/developing-in-modx/advanced-development/developing-rest-servers

        Thank you Bob sir. You always help me to figure out things. Can you help me more ... lil bit more in this to understand how it works really.. ?

        Like I want to get data from a page which's url is exmaple.com/mypage then how I connect RESTful to GEt from this ? Also from chunks ?
          My Proudly Powerd By Modx site - http://irresistiblemt.in smiley Its simple site but its made in best CMS.
          • 3749
          • 24,544 Posts
          If it's a simple app, you would typically have only one resource with a snippet that handles all requests. The snippet would do different things depending on the rest action (GET, POST, etc.) and sometimes on the URL or the query string.

          There is a built-in Rest class in MODX, but I've never used it. I've used external RESTful APIs, but only as a client, I've never created one.

          The files are in this directory:

          core/model/modx/rest/


          I suspect that Package Manager uses REST to check for updates.
            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
            • 18809
            • 109 Posts
            Quote from: BobRay at Oct 08, 2017, 03:35 AM
            If it's a simple app, you would typically have only one resource with a snippet that handles all requests. The snippet would do different things depending on the rest action (GET, POST, etc.) and sometimes on the URL or the query string.

            There is a built-in Rest class in MODX, but I've never used it. I've used external RESTful APIs, but only as a client, I've never created one.

            The files are in this directory:

            core/model/modx/rest/


            I suspect that Package Manager uses REST to check for updates.



            How to make a simple snippet to Get from a resource ? I am too dumb to figure it out. :/
              My Proudly Powerd By Modx site - http://irresistiblemt.in smiley Its simple site but its made in best CMS.
              • 18809
              • 109 Posts
              Hey Bob sir, Can you teach me or give me an example on how to do this correctly ? I am new in api stuffs on Modx.
              Here is the url of my demo site where I installed REST with example as read by instructions
              (https://gist.github.com/splittingred/2346752)

              - http://irresistiblemt.in/mymodx/rest/box

              And here is the url of my demo site's main page which's data I want to fetch for my android app.
              [url]http://irresistiblemt.in/mymodx/[/url]


              I want to get the post and tags also from my BlogIt blog. I dont want to move to wordpress for this even its easy in wordpress with rest api plugin but I want to do this in Modx.

              Please help me in this sir. _/\_ You are the only one who reply to my problems. There is no one who cares here. Modx community is weak to help. sad
                My Proudly Powerd By Modx site - http://irresistiblemt.in smiley Its simple site but its made in best CMS.
                • 3749
                • 24,544 Posts
                As I mentioned above, I've never use the MODX REST class, and I don't have time to learn about it right now.

                This might get you started on a snippet that just takes the incoming information and returns a JSON string:


                Start with this in the snippet:

                return print_r($_GET, true);


                That will show you what's in the $_GET array when reached with various things in the query string.

                Then you can use that information to create your response:


                $var1 = $_GET['something'];
                $var2 = $_GET['something_else'];
                
                $response = array();
                
                /* Get the information requested and put it into the $response array */
                
                return $modx->toJSON($response);
                




                  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
                • The modRestService/modRestController things on https://docs.modx.com/revolution/2.x/developing-in-modx/advanced-development/developing-rest-servers are very straightforward.

                  Just create everything as mentioned there, use 'modResource' as $classKey in your controllers and you could retrieve (better don't open put/post/delete there) your resources with simple /rest/{controllername} and one resource with /rest/{controllername}/{resource_id}.

                  If you need to retrieve the tags (do you use Tagger for his?) and the TVs of the blogpost, add the information in a custom afterRead method in your controller class. [ed. note: Jako last edited this post 6 years, 5 months ago.]
                    • 18809
                    • 109 Posts
                    Quote from: Jako at Oct 09, 2017, 11:38 AM
                    The modRestService/modRestController things on https://docs.modx.com/revolution/2.x/developing-in-modx/advanced-development/developing-rest-servers are very straightforward.

                    Just create everything as mentioned there, use 'modResource' as $classKey in your controllers and you could retrieve (better don't open put/post/delete there) your resources with simple /rest/{controllername} and one resource with /rest/{controllername}/{resource_id}.

                    If you need to retrieve the tags (do you use Tagger for his?) and the TVs of the blogpost, add the information in a custom afterRead method in your controller class.

                    Yes blogIt Extra use Tagger for Tags and I really new in this controller thing so I cant figure out myself.

                    Here is the codes -

                    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 = 'modResource';
                        public $defaultSortField = 'id';
                        public $defaultSortDirection = 'DESC';
                    }
                    


                    rest folder's index.php -

                    <?php
                    /**
                     * Place in /rest/ directory of site. Add .htaccess file above. Note that our REST controllers are in /rest/Controllers/; they can be anywhere though - just change the basePath config option.
                     */
                    
                    /* first load MODX externally */
                    require_once 'my-public_html/mymodx/config.core.php';
                    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
                    $modx = modX::getInstance('rest');
                    $modx->addPackage('myblog','my-public_html/mymodx/core/model/');
                    
                    /* now load the REST service */
                    $rest = $modx->getService('rest','rest.modRestService','',array(
                        'basePath' => dirname(__FILE__).'/Controllers/',
                        'controllerClassSeparator' => '',
                        'controllerClassPrefix' => 'myControllers',
                        'xmlRootNode' => 'response',
                    ));
                    $rest->prepare();
                    if (!$rest->checkPermissions()) {
                        $rest->sendUnauthorized(true);
                    }
                    $rest->process();


                    Result :

                    success	false
                    message	"Method not allowed"
                    object	[]
                    code	405


                    I cant even found a single working example which I can check to figure out how these things works.
                    Please help me. I just want to get data from my BlogIt posts, tags and normal modx resources to make android apps with REST Api.

                    http://irresistiblemt.in/appwp/wp-json/wp/v2/posts?fields=id,title,content,featured_media
                    Here we see that we can fetch id, title, content etc fields and or if we call /posts then we can pull everything.

                    I want like this(Wordpress) in Modx <3
                      My Proudly Powerd By Modx site - http://irresistiblemt.in smiley Its simple site but its made in best CMS.
                    • Your code should work. I think that box.php is not at the right position. The following files should be there:

                      /rest/index.php
                      /rest/Controllers/box.php

                      Since you don't want to allow post/put/delete calls, you have to add some methods to box.php

                      <?php
                      /**
                       * Found at: rest/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 = 'modResource';
                          public $defaultSortField = 'id';
                          public $defaultSortDirection = 'DESC';
                      
                          public function beforePost()
                          {
                              return false;
                          }
                      
                          public function beforePut()
                          {
                              return false;
                          }
                      
                          public function beforeDelete()
                          {
                              return false;
                          }
                      
                      }