We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26104
    • 36 Posts
    Hello,

    For a simple project I want to pass variables through the url. What i would like to achieve is:
    http://www.mysite.com/site/category/regional in which "site" is the alias of the page which is called from ModX, "category the name of the variable and "regional" is the variable I want to use in my snippets. Do you have any idea about how I could achieve this?

    Gr,
    Sander.
      • 22303 MODX Staff
      • 10,725 Posts
      Using some regular expressions and the mod_rewrite engine. You would need to supersede the existing rules for your particular conditions (i.e. when q=site, convert the remaining URL into query string parameter pairs, or similar).
        • 26104
        • 36 Posts
        Well I was thinking more in the direction of somekind of plugin which would use the settings of the friendly urls (with or without alias paths) to determine which parameters in the URL are page-based and which are added. Then you can strip that part from the URL and then fetch the rest in to an array with the first value as the key, second as the value.

        Do you think this would be easy to realise? I’m a big fan of ModX Modules and plugins and we build a lot of modules for ourselves and our customers, but I never used plugins...
          • 26104
          • 36 Posts
          Started doing some tests with a simple plugin and a snippet. Unfortunately when I start to add extra parameters behind the URL after the page, ModX will allways go to the root of the site. So I think I also have to write some kind of redirecting plugin which only looks at the first part of the url. Anyone got an idea?

            • 26104
            • 36 Posts
            Seems like I’m talking to myself but I’m still going strong and I have a solution (which could be integrated in next versions of modX maybe?):

            1: I created a plugin to convert the request uri into an array. Because you need keys and values I chose this way:
            myurl.com/key1/var1/key2/var2 converted into an array: array("key1" => "var1", "key2" => "var2");

            The plugin:
            if(!empty($_SERVER['REQUEST_URI'])) { 
            	$get = explode('/', substr($_SERVER['REQUEST_URI'], 1)); 
            
            	for ($i = 0; $i < count($get); $i++)
            		if ($i % 2)
            			$url[$key] = $get[$i];
            		else
            			$key = $get[$i];
            	
            } // end if
            
            
            $modx->urlVars = $url;

            // This plugin is in development, any tips on how things can be done in a more easy way are very welcome

            Works fine on the homepage but as soon as I navigate to a subpage and add use an url like http://www.mysite.nl/alias/key1/val1, things stopped working, I got redirected to the homepage. So I started to look in the documentparser class: manager/includes/document.parser.class.inc.php

            On rule 323 I found the function which is responsible for translating the URL into the correct pageID or alias. To achieve my goals I changed the following (this code block starts on line 332):
                    if ($this->config['use_alias_path'] == 1) {
                        $this->virtualDir= dirname($q);
                        $this->virtualDir= ($this->virtualDir == '.' ? '' : $this->virtualDir);
                        $q= basename($q);
                    } else {
                        $this->virtualDir= '';
                    }
            


            into
                    if ($this->config['use_alias_path'] == 1) {
                        $this->virtualDir= dirname($q);
                        $this->virtualDir= ($this->virtualDir == '.' ? '' : $this->virtualDir);
                        $q= basename($q);
                    } else {
                        $this->virtualDir= '';
                        $arr = explode('/',$q);
                        $q = $arr[0];
                    }
            


            Notice the changed else statement. What this code does is exploding the queryURL and stripping everything except for the item after the first /. Unfortunately this can’t be achieved easily when working with alias_paths. So i’m still thinking about a solution for this.

            What do you think of my solution? Did I overlooked anything?

            Now I’m continuing to develop the plugin, if anyone is interested I’ll post it here when i’m done (or getting further tongue). But i’ll see that when someone ends my monologue in this thread...
              • 400
              • 40 Posts
              I’m grin
                • 26104
                • 36 Posts
                Updated version! smiley

                I added 2 config parameters, the first one will slice of the alias of the array, if set. This is not yet fully functional because now it slices of the first piece of the array, no matter what. Have to build some check or something to check if the document is root or something so the first key won’t be stripped then. The second parameter is for compatibility usage, if you have a script and want to make it compatible, this parameter makes it possible to keep on using the old $_GET calls in the snippets.

                Todo:
                - Securing the input using htmlspecialchars etc
                - Fix the alias bug (as described)
                - Making sure that if ?blaat=blaat or something like that is added to the end of the url it will be ignored

                // Checking if the REQUEST_URI is set:
                if(!empty($_SERVER['REQUEST_URI'])) 
                { 
                	// Put the request URI into a var:
                	$url = $_SERVER['REQUEST_URI'];
                	
                	// Check if the URI has a trailing slash, if not: Add one:
                	if (substr($url, -1) !== '/')
                	{
                		$url = $url . '/';
                	}
                		
                	// Remove the standard first slash:
                	$url = substr($url, 1);
                	
                	// Explode the request URI on /
                	$get = explode('/', $url); 
                	
                	// If the alias may not be set as key: skip the first item
                	if($alias_as_key == 0)
                	{
                		// Shift off the first item
                		array_shift($get);
                	} // end if
                	
                	// Walking through the array and parsing it into a new array
                	for ($i = 0; $i < count($get); $i++)
                	{
                		// Parse the array one by one, the first time the $key = saved, 
                		// the next time the $key + the $get[$i] are saved into the target array
                		if ($i % 2)
                		{
                			$vars[$key] = $get[$i];
                		} else {
                			$key = $get[$i];
                		} // end if
                	} // end for		
                } // end if
                
                if($define_GETs == 1)
                {
                	array_push($_GET, $vars);
                }
                
                // Send the $vars array to a global modx var:
                $modx->urlVars = $vars;
                


                Configuration:
                &alias_as_key=Use Alias as key in array;int;0 
                &define_GETs=Define vars as $_GET;int;0
                


                System events:

                • OnParseDocument
                • OnWebPagePrerender

                And if you want to use this you also have to make the change in the document parser class as mentioned in one of my previous posts, would be nice if this could be implemented in modx in the future. because it won’t do any harm in my opinion?
                  • 27376
                  • 576 Posts
                  <?php
                  // Walking through the array and parsing it into a new array
                  	for ($i = 0; $i < count($get); $i += 2) {
                  			// $key = $get[$i];
                  			// $value = $get[($i+1)];
                  			$vars[$get[$i]] = $get[($i+1)];
                  	} // end for
                  ?>

                  Simpler and faster grin
                    • 26104
                    • 36 Posts
                    Doh... thanks... smiley Wasn’t very sharp when I wrote that piece of code.