We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25586
    • 105 Posts
    Hi I'm working on a website and need a costum friendy-url. For this i'm using the OnPageNotFound handler but I can't het it to work. The following code doesn't work.

    <?php
    if ($modx->event->name == 'OnPageNotFound') {
    	$uri = trim($_SERVER['REQUEST_URI'], '/');
    	$part = explode('/', $uri);
    	
    	if ($part[0] === 'location') {
    	
    		$resourceId = 25;
    		
    		$location = $modx->getObject('Location', array(
    			'uri' => $uri
    		));
    		
    		$locationId = $location->get('id');
    	
    	    if(intval($locationId)) {
    	        $_GET['id'] = $locationId;
    	        $modx->sendForward($resourceId);
    	    }
    	}
    }


    The page is white and the PHP errorlog says that the GET command isn't a valid object. I tried calling my service but that doens't work eather.

    Anyone knows how to do this? [ed. note: rover87 last edited this post 14 years, 6 months ago.]
      • 3749
      • 24,544 Posts
      Try getObject('modResource' . . .)

      There is no 'Location' object in the DB unless you created one.

      You may also have to look in at the uri field in the DB and examine your $uri variable to make sure the form will match.

      BTW, you don't need the first 'if' statement if your plugin is only connected to one event.


      ---------------------------------------------------------------------------------------------------------------
      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
        • 25586
        • 105 Posts
        I created a snippet and schema for 'Location' which works fine with formIt, I can add data to the table but can't use it here it seems.

        I'm using MODX Revolution 2.2.0-pl2 (traditional) smiley
          • 25586
          • 105 Posts
          To be more specific, i got the code from his tread (https://forums.modx.com/thread/?thread=35476&page=1) but it contains:
          <?php
          $productId = $modx->db->getValue("SELECT `product_id` FROM `product` WHERE `alias` = '{$requestParts[1]}'");
          ?>
          which is bad code. I even tried that but it says "Call to a member function getValue() on a non-object" sad

          With my code the "get" function "Call to a member function get() on a non-object"

          Hope someone can help me out
            • 25586
            • 105 Posts
            I got it! I think I needed some sleep yesterday because $uri = trim($_SERVER['REQUEST_URI'], '/') trims the '/' from beginning AND end. My uri whas incomplete so there never was a object just like the PHP error said.