We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48243
    • 12 Posts
    I am trying to optimize my website by reducing the amount of requests made. I have custom logging throughout every snippet and bit of code on my site, and noticed that on a single page, it actually loads twice. I checked every other page and it seems the page is loaded then refreshed on its own. I assumed it could have been my gateway plugin but could not find an issue. I am assuming that in my context switching snippet is causing the problem. can someone give me some input on this issue or if this has happened to you?
    <?php
    $modx->setLogLevel(1);
    
    function setContext($context, $key, $value)
    {
    	global $modx;
    	$username = $modx->user->get('username');
    	if(checkArgs($context, $key, $value)) 
    	{
    		if(is_array($key) && is_array($value)) 
    		{
    			$i = 0;
    			foreach($key as $label) {
    				$setting = $modx->getObject('modContextSetting', array('context_key'=>$context, 'key'=>$key[$i]));
    				if($setting) 
    				{
    					$setting->set('value', $value[$i]);
    					$setting->save();
    				}
    				$cSetting[] = array($key[$i], $value[$i]);
    				$i++;
    			}
    			// $modx->log(4, $username . print_r($cSetting),'', 'contextSetter');
    		}
    		else
    		{
    			$setting = $modx->getObject('modContextSetting', array('context_key' => $context, 'key' => $key));
    			if($setting) 
    			{
    				$setting->set('value', $value);
    				$setting->save();
    			}
    			// $modx->log(4, $username . print_r(array($key, $value)),'', 'contextSetter');
    		}
    		$modx->switchContext($context);
    	}
    	else 
    	{
    		$modx->switchContext($context);
    	}
    	$cacheRefreshOptions =  array( 'context_settings' => array() );
    	$modx->cacheManager->refresh($cacheRefreshOptions);
    }
    
    function checkArgs($context, $key, $value)
    {
    	if(!empty($context) && !empty($key) && !empty($value)){
    		return TRUE;
    	} else {
    		return FALSE;
    	}
    }
    ?>



    Here is my gateway plugin
    <?php
    try {
        $modx->setLogLevel(4);
    
        include(MODX_BASE_PATH . "assets/snippets/sourceDetection.php");
        include(MODX_BASE_PATH . "assets/snippets/contextSetter.php");
        require(MODX_BASE_PATH . "assets/plugins/http_host-index.php");
        /* Don't execute any gateway code if Modx Manager is the target context */
        if ($modx->context->get('key') == 'mgr') {
            return;
        }
        
        /***************************************************************
         * Detect host name and direct to the appropriate modx context.
         ***************************************************************/
        $http_host = $modx->getOption('http_host');
    
        if(array_key_exists($http_host, $hosts_array)) {
            $host     = $hosts_array[$http_host];
            // $modx->log(4,'HTTP HOST: ' . print_r($host,true),'','gateway');
            $context  = $host[0];
            $key      = $host[1];
            $value    = $host[2];
        } else {
            $context  = 'web';
            $key      = '';
            $value    = '';
        }
        /***************************************************************
        * Set Context
        ***************************************************************/
        setContext($context, $key, $value);
        /***************************************************************
         * Set Source Data.
         ***************************************************************/
        setSource();
    }
    catch (Exception $e) {
        $modx->log(modx::LOG_LEVEL_ERROR,'General Exception: '.$e,'','gateway');
    }
    ?>



    My gateway plugin is being fired on the OnHandleRequest system event