We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37393
    • 68 Posts
    Hi,
    can someone tell me how to get the requested page (by alias) by using the $_GET method,

    These are my snippets (Does only get the page´s id), e.g /page?pageID=51

    GetPageId snippet

    <?php
    return $_GET['pageID'];



    PageCall snippet

    <?php
    if (!isset ($pageID))  {
    	return "The page with the requested id does not exist!";
    }
    else {
    	$res = $modx->getObject('modResource', $pageID);
    	return ($res->get('content'));
    }


    On the page i am calling the snippet PageCall:
    [[!PageCall? &pageID=`[[!GetPageId]]`]]


    Is it possible to read a page´s alias from a array then pass it into the GET parameter so the user can request it by typing "/page?page=random-page in the url instead of the page´s id?

    $docArray = $modx->getObject('modResource',array( 
    'published' => 1,
    'alias' => 'first-page',
    'published' => 1,
    'alias' => 'second-page',
    ));
    return ($docArray->get('content'));


    Any ideas, folks? Any suggestion are greatful. wink

    Friendly URLs Enabled
    MODx Revolution 2.2.0pl2
      IntoVPS - VPS hosting service with super-fast response.
      If MODx does not suit your needs then you have a lack of imagination.
      • 3749
      • 24,544 Posts
      If I'm understanding you, you could use this:


      $modx->getObject('modResource', array('alias' => $_GET['pageAlias'));



      It would not be as fast as getting the page by ID, but I doubt if you'd notice the difference.

      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 37393
        • 68 Posts
        Works fantastic Bob, but this allows any page to be outputted. I was looking to pass the allowed pages within the array and then validate if they exist by using the in_array method. Is that possible?
        etc:

        $pages = array("Page1", "Page2", "Page3");
        if (in_array("Page1", $pages)) {
            return ($modx->get('content'));
        }
        else{
        return "That page does not exist within this section!";
        }


        Could you help me embedding this code into the previous code snippets that both you and me wrote above? [ed. note: bowkilled last edited this post 14 years, 7 months ago.]
          IntoVPS - VPS hosting service with super-fast response.
          If MODx does not suit your needs then you have a lack of imagination.
          • 37393
          • 68 Posts
          I have made some code but it does not seem to work..

          $res = $modx->getObject('modResource', array(
          'alias' => $_GET['pageAlias']
          ));
          if(isset($res['alias']['example-page'])){
              return ($modx->get('content'));
          }
          elseif(!isset($res['alias']['example-page'])){
          return "That page does not exist within this section!";
          }
          }


          What im trying to do is to use a method like "in_array" or a "if-else isset" to check if a key´s value matches a valid one and then return the output from that page.

          etc above, the isset function looks for a page with the alias "example-page", if that page with the requested alias exists, then it would return the page.
            IntoVPS - VPS hosting service with super-fast response.
            If MODx does not suit your needs then you have a lack of imagination.
            • 3749
            • 24,544 Posts
            Assuming that you have a comma-separated list of legal aliases in a snippet property like this:

            &legalAliases=`home,example-page,otherpage`

            You could do something like this (untested):


            <?php
            $legalAliases = explode(',' , $scriptProperties['legalAliases']);
             
            if (in_array($_GET['pageAlias'], $legalAliases, true) ) {
                $res = $modx->getObject('modResource', array(
            'alias' => $_GET['pageAlias']));
                    
                if ($res) {
                    return $res->getContent();
                } else {
                   return 'Resource not found';
                }
            } else {
                return "That page does not exist within this section!";
            }





            }




            ---------------------------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
            MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 7 months 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
              • 37393
              • 68 Posts
              The one you posted above does not seem to work but i have modified your code and it works now:

              $res = $modx->getObject('modResource',array(
              'alias' => $_GET['pageAlias']
              ));
              $actions = explode(",", $scriptProperties['actions']);
              
              if(in_array($_GET['pageAlias'],$actions)){
                  return ($res->get('content'));
                  }
                  else
                  {
                  echo "That page does not exist within this section!";
                  }


              This works great.
              Thanks, Bob!
                IntoVPS - VPS hosting service with super-fast response.
                If MODx does not suit your needs then you have a lack of imagination.
                • 3749
                • 24,544 Posts
                Sorry, my code had some typos in it. I've corrected it.

                For your code, I'd recommend moving the getObject() call inside the if statement, since there's no point in making the user wait while you get the resource if you're not going to use it.

                You should also test the return from getObject() before calling its get() method. Otherwise, there's the potential for the user to end up with a PHP error on the screen. Something like this:

                <?php
                
                $actions = explode(",", $scriptProperties['actions']);
                
                if (in_array($_GET['pageAlias'], $actions)) {
                
                    $res = $modx->getObject('modResource', array(
                        'alias' => $_GET['pageAlias']
                    ));
                
                    if (!$res) {
                        return 'Resource not found';
                    } else {
                        return ($res->get('content'));
                    }
                } else {
                    echo "That page does not exist within this section!";
                }



                ---------------------------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                MODX info for everyone: http://bobsguides.com/modx.html
                  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