We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14536
    • 22 Posts
    For example, I’m working on a store snippet for MODx right now. Currently, products show as:
    /store.html?productId=34
    But I would love to have it be:
    /store/t-shirts/v-neck.html

    Is this possible on the fly? Or would it require creation of an actual page for each product?
      • 22303 MODX Staff
      • 10,725 Posts
      You can author a plugin that uses $modx->sendForward() and is registered to the OnPageNotFound event that does whatever you want. Here is an example:
      <?php
      /*
      this should be the Resource id of the page containing your view Snippet
      */
      $productResourceId = 21;
      
      $requestURI = trim($_SERVER['REQUEST_URI'], '/');
      $requestParts = explode('/', $requestURI, 2);
      if (count($requestParts) == 2) {
          if ($requestParts[0] === 'store') {
              /* 
              this assumes you have a product table with an alias field for specifying the relative product URL as t-shirts/v-neck.html
              */
              $productId = $modx->db->getValue("SELECT `product_id` FROM `product` WHERE `alias` = '{$requestParts[1]}'");
              if (intval($productId)) {
                  /*
                  set the productId into the $_GET variable for your product view Snippet to use
                  */
                  $_GET['productId'] = $productId;
                  /*
                  now forward the request to your Resource containing the product view Snippet
                  (forwarding does not change the requested URL like a redirect)
                  */
                  $modx->sendForward($productResourceId);
              }
          }
      }
      
      ?>
        • 14536
        • 22 Posts
        Awesome. Thank you so much for the informative response, I’m glad I’m working with MODx when stuff like this happens, haha. I’ll be releasing the snippet for public release soon, hopefully tomorrow. I have a couple more questions, should I keep them in this thread or should I start another?

        If the former, I’ll go ahead and ask them right now:

        In my snippet, for ease of use, I create another MODx document for the manager, and may want to create more later. I can see that it is inserted into MySQL, but doesn’t show up in the front end or the manager. What would I have to do
        that? (and yes, I’ve read the thread about this not being entirely kosher yet...so I won’t be hurt if you say wait)

        Also, now that you’ve shown me the way to go on a plugin, will I have similar issues with creating the plugin, or will it just go?

        I had more but that’s it for now.

          • 14536
          • 22 Posts
          Staying off topic, but conserving threads: I’d really like to be able to create pages for the snippet, so I’m investigating further. When I added another page using the manager, the page I tried to create showed up in the site tree. Is this related to updating the cache, or something else I can look into?
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: johnnys at Oct 29, 2009, 03:47 PM

            Staying off topic, but conserving threads: I’d really like to be able to create pages for the snippet, so I’m investigating further. When I added another page using the manager, the page I tried to create showed up in the site tree. Is this related to updating the cache, or something else I can look into?
            Yes, it shows up because the site cache is cleared when you save a document in the manager (unless you uncheck the box that indicates you want to clear the cache on save). See this post for some more information: http://modxcms.com/forums/index.php/topic,33244.msg204502.html#msg204502
              • 13247
              • 3 Posts
              I’m a little concerned about how this would work if someone know how the code was written and tried to send the store someting it did not expect. If you do not have magic quotes turned on, this query will allow people on the net to manipulate your query. This will lead to a possible SQL Injection attack. NEVER send not sanitized strings originating from a remote user to the database.

              This is bad code.
              Quote from: OpenGeek at Oct 27, 2009, 10:19 PM

              <?php
              $productId = $modx->db->getValue("SELECT `product_id` FROM `product` WHERE `alias` = '{$requestParts[1]}'");
              ?>

                • 22303 MODX Staff
                • 10,725 Posts
                First of all, all requests to modx are sanitized automatically, but you should definitely add validation for your specific requirements. I was only providing an example of how to accomplish it as quickly as I could. You would have to customize this script anyway. Further, in Revo, database injection on this stuff is a non-issue if you use xPDO or PDO prepared statements.

                Second, with the way the REQUEST_URI handling is done there, it would be difficult to get SQL injection in, but point taken none-the-less.
                  • 14536
                  • 22 Posts
                  Hey Jason, I’m working on this, but it doesn’t seem to be going through correctly... or the timing is off.

                  In the plugin, I am setting $_GET, as you showed. Here’s the output of print_r($_GET) from within the plugin:
                  Array
                  (
                      [q] => store/Backpacks/Burton Bag.html
                      [mode] => single
                      [product] => Burton Bag
                      [category] => Backpacks
                  )
                  


                  All good, just what I want. Back in the snippet, all I am getting is this:
                  Array
                  (
                      [q] => store/Backpacks/Burton Bag.html
                  )


                  I’m not sure what is causing this, any hints?
                    • 22303 MODX Staff
                    • 10,725 Posts
                    Did you set those extra GET vars in the plugin after finding a match?
                      • 14536
                      • 22 Posts
                      Yeah. They are shown in the first code snippet. The case I’m looking at is this:
                      $getData = array(
                      	'mode' => 'single',
                      	'product' => str_replace($this->modx->config['friendly_url_suffix'], '', urldecode(array_pop($requestParts))),
                      	'category' => urldecode(array_pop($requestParts))
                      );
                      			
                      
                      //set data
                      foreach($getData as $key => $value){
                      	$_GET[$key] = $value;
                      }


                      Which is leading to the (correct) output of the first code snippet in the post above.