We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36416
    • 589 Posts
    Quote from: Rogier at Jul 14, 2010, 01:05 AM

    I noticed this is for Evolution? Can I use this in Revolution?

    No - sorry, I missed "Next" in the name of the forum...
      • 34127
      • 135 Posts
      Well, your external PHP file would be your "connector" in this case, and some code like this should work (untested):

      <?php
      
      $basePath = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) );
      
      /* Allow anonymous users */
      define( 'MODX_REQP', false );
      
      /* Require MODx configuration, and base connector file */
      require_once( $basePath . '/config.core.php' );
      require_once( MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php' );
      require_once( MODX_CONNECTORS_PATH . 'index.php' );
      
      /* Add your own initialization code for your component here */
      
      /* Handle request */
      $processorsPath = $modx->getOption( 'core_path' ) . 'components/MY_COMPONENT/processors/';
      
      $modx->request->handleRequest( array(
          'processors_path'   => $processorsPath,
          'location'          => 'web'
      ) );
      
      ?>
      


      A couple of notes:

      1. The ’MODX_REQP’ define at the top allows non-validated users to access the connector. Remove this if only logged-in users should have access to this connector.
      2. $basePath is the path to the MODx base. This example assumes your connector is located at /assets/components/MY_COMPONENT/connectors/my-connector.php
      3. handleRequest()’s ’location’ parameter lets you [optionally] define a sub-directory of whatever the processors_path is. I generally store all manager processors inside processors/mgr and my web connectors inside processors/web - adjust accordingly. Now, passing an ’action’ request parameter to the connector will load up the appropriate processor. For example, passing "getlist" will load up the getlist processor inside your component’s processors/web/ directory.
        • 28215
        • 4,149 Posts
        fyi, using MODX_REQP is not recommended unless you absolutely need to allow anonymous users, since that means anyone who knows the URL could see that data. If you do use it, make sure you’re not transmitting any sensitive data.
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 25586
          • 105 Posts
          Thank you so much for the help so far! I can make ajax calls with my snippet! grin

          Right now I’m trying to get the menu working with ajax. Pages are called by #page or #page/subpage/ by the wayfinder snippet.

          My ajax .js file removes the # from #page so my variable in my php script is $page. How can I get my pagerequest.php load in the proper content?

          I think something like this:

          Get modx pages aliasses -> find same alias as $page and export the document content.

          How can I accomplish this with modx? I looked @ wayfinder but I’m not a coder by profession so I hope somone can help me out with my final challenge!

          Thanks for the amazing help so far!

          Rogier
            • 25586
            • 105 Posts
            Hmm should have waited because just I was typing I got it in my head and got it working now.

            The following code did the trick:
            $url = $_POST['url'];
              list($page, $group) = split('/', $url);
            
            $document = $modx->getObject('modResource',array(
                'alias' => $page,
            ));
            
            echo $document->get('content')."<br />\n";


            Improvements are welcome wink

            Rogier

            EDIT:

            If $modx->getObject gets the page, how can I get the content of a page in a folder? #page/page/ for example?

            Haven’t figured this out!

            Rogier
              • 25586
              • 105 Posts
              I noticed that if I get the content of a page with ajax, the snippet on that page wont get executed. How can I accomplish this? The output is just my call: [[!mySnippet]]

                • 28042 ☆ A M B ☆
                • 24,524 Posts
                That depends on how you are fetching the page. If you’re just getting the content field from the database, then it won’t get parsed. But if you put your snippet in a regular resource with no template, and use the URL to the resource as the AJAX source, then it will be parsed like any other requested resource.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 25586
                  • 105 Posts
                  Hoped there was another way. Right now my website looks like this:

                  www.mysite.com/#page/ = with ajax
                  www.mysite.com/page/ = without ajax

                  Google can search the /page/ and when you have javascript enabled you will be redirected to /#page/

                  When I use the /page/ for my snippet call, the page wont be visible for users without javascript.

                  Hope you know a solution for this smiley

                  Rogier
                    • 29019 ☆ A M B ☆
                    • 104 Posts
                    An easy way to handle non-javascript users (and search engines) is to use standard links, but change them all with javascript once the page loads to use your formatted links. The only hitch is that some links shouldn’t get formatted (an off-site link, or a file) while others should. I use the rel attribute for this.

                    Here’s a (dramatically over-simplified) example:

                    <html>
                    <head>
                    	...
                    	<script type="text/javascript">
                    		function fix_links() {
                    			// this is for prototype, but it's easy to translate
                    			$$('a:not([rel=fixed])').each(function(s) {
                    				if (s.rel == 'off-site') {
                    					s.setAttribute('target', '_blank'); // or a javascript open page
                    				} else {					
                    					var new_href = "your_ajax_get_page_function('"+s.href+"');";
                    					
                    					s.setAttribute('onclick', new_href + " this.blur(); return false;");
                    					if (/MSIE\s7/.test(navigator.userAgent)) {
                    						if (s.href.indexOf('javascript:') == -1) {
                    							s.setAttribute('href','javascript:' + new_href);
                    						}
                    					}				
                    				}
                    				s.setAttribute('rel', 'fixed');
                    			});
                    		}
                    		
                    		// call your fix_links function on page load:
                    		document.observe("dom:loaded", fix_links);
                    	</script>
                    </head>
                    <body>
                    	<p><a href="page-one.html">Page One</a>, <a href="page-two.html">Page Two</a>, and <a href="page-three.html">Page Three</a> are all ajax pages.</p>
                    </body>
                    </html>


                    Every page’s template would want that bit of javascript in there, but other than that it should be a standard page that’s easy for humans and search engine’s to parse.
                      www.darkstardesign.com

                      Are you in the Nashville area? Join us for our next MODX meetup!