We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14911
    • 7 Posts
    I’ve been working on a work-around to fix modMobile (http://rtfm.modx.com/display/ADDON/modMobile) since the 2.1 caching corrections. I am using MODx version 2.1.1 on this particular site. So far, I’ve attempted the suggestion posted by Mark H. here: https://github.com/jkenters/modMobile/issues/2, by using $modx->sendForward();. I was able to get the request to forward to a specific uncached resource, but when I attempt to retrive the id or unprocessed content of the requested resource, MODx seems to simply output the processed resource content. I have found that I can use
    $mResource = $modx->getObject('modResource', id); $mResource->getContent()
    and it processes the unprocessed content as if it were part of the resource I forwarded the request to, but I can’t seem to figure out how to get the resource ID from the request without MODx processing the resource and request. Interestingly, MODx also processes the resource normally when chaining the code above, like this:
    $mContent = $modx->getObject('modResource', id)->getContent();


    MODx also processes the resource (completely handles the request) when I do something like so:
    $modx->request->getResourceIdentifier();


    Any help would be appreciated. Thanks in advance!
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      What about $resource->_content? That should give the raw or cached output I think.

      What do you mean with processing and handling the request? That could be interpreted in many different ways tongue
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 14911
        • 7 Posts
        Thanks for your reply.

        Processed: raw content parsed by MODx, ready for output.
        Handling the request: processes the original request, regardless of forwarding. MODx parses and outputs the requested resource as if it hadn’t been forwarded.

        No matter, I got it to work, but I’d rather have a less hackish solution. It seems the main issue was the event that the plugin was associated with. Instead of OnHandleRequest (as you suggested), I used OnWebPageInit. This event gets fired just after the resource identifier is set in modRequest. Here’s the rest of what I did:

        Plugin:
        <?php
        /*
         * modMobile plugin
         *
         * Package by: Jeroen Kenters / www.kenters.com
         * Based on code of Danny Herran (http://www.dannyherran.com/2011/02/detect-mobile-browseruser-agent-with-php-ipad-iphone-blackberry-and-others/)
         *
         * License: GNU GENERAL PUBLIC LICENSE Version 2, June 1991
         *
         * */
        
        //get mobile theme setting
        $mobile_template = $modx->getOption('mobile_template');
        
        //only proceed when there is a mobile theme
        if($mobile_template)
        {
            $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
        
            $mobile_browser = '0'; //default: not mobile
        
            if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
                $mobile_browser++;
        
            if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
                $mobile_browser++;
        
            if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
                $mobile_browser++;
        
            if(isset($_SERVER['HTTP_PROFILE']))
                $mobile_browser++;
        
            $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
            $mobile_agents = array(
                                'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
                                'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
                                'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
                                'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
                                'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
                                'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
                                'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
                                'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
                                'wapr','webc','winw','winw','xda','xda-'
                                );
        
            if(in_array($mobile_ua, $mobile_agents))
                $mobile_browser++;
        
            if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
                $mobile_browser++;
        
            if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'ipad') !== false)
                $mobile_browser++;
        
            if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
                $mobile_browser=0;
        
            //change template if mobile browser is found
            if($mobile_browser>0) {
              //$modx->resource->set('template', $mobile_template);
              //$modx->resource->set('context_key', 'mobile');
              $_GET['mResourceMethod'] = $modx->request->modx->resourceMethod;
              $_GET['mResourceIdentifier'] = $modx->request->modx->resourceIdentifier;
              $modx->sendForward(27);
            }
        }
        return true;



        Snippet (called from the resource the request gets forwarded to):

        //get resource we're requesting
        $mResourceMethod = $_GET['mResourceMethod'];
        $mResourceIdentifier = $_GET['mResourceIdentifier'];
        
        $mResource = $modx->getObject('modResource',($mResourceMethod == 'id' ? $mResourceIdentifier : array($mResourceMethod => $mResourceIdentifier)));
        
        //gets resource content
        $mResourceContent = $mResource->getContent();
        
        //output retrieved resource
        echo $mResourceContent;


        Also, the resource that receives the forwarded request is uncached and has the template set to the mobile template.

        This works, but I’d rather have it done right. Caching would be nice, and my first thought was to simply choose a different cache path (along with the template switch) when a user with a mobile device visits the site. This would allow for caching, and be very simple. Do you have any ideas?
          • 24068
          • 4 Posts
          Quote from: jsewill at Jul 12, 2011, 03:21 PM

          Do you have any ideas?

          Yes, just leave the modMobile path to present layouts for mobile devices and use CSS for that.

          Topic about that: http://modxcms.com/forums/index.php/topic,64970.0.html

          If you need help with this, post back here.
            • 14911
            • 7 Posts
            If it were my call to make, I might build this site from the ground up with something like that. However, that is not the case. I was tasked with this by a web shop I do work for. Also, I’d imagine many designers/developers would like a plugin that would accomplish this (including myself). Thanks for your time and thoughts.
              • 21191
              • 236 Posts
              I’ll be honest Abraham works for us smiley He’s working on trying to get jQuery Mobile working. To my understanding that won’t really work as a CSS only media type.

              This recent smashing magazine article has some relevance as well http://www.smashingmagazine.com/2011/07/11/picking-a-mobile-support-strategy-for-your-website/

              Right now we’ve tried a few approaches.

              1 - Created a separate context for mobile, applied a template(jquery mobile), works well, caching works on both, nice system. However we’d need to duplicate content. I don’t like this idea, if you have a work around, please share!

              2 - Let’s use ModMobile to on-the-fly switch the template. Works well, except of the caching issues. https://github.com/jkenters/modMobile/issues/2

              3 - we tried a somewhat hybrid approach (Abe did) keeping the caching for normal, but using some ajax magic to get mobile happy.

              Our end goal here is to have the ability to sort of "Bolt On" a mobile App to nearly any site. Mod Mobile came close, but I also like the idea of using the m.domain.com as a convention for your mobile site. I also don’t like the idea of creating a smaller subset of your site for mobile because so often we read content from blogs or whatever on mobile devices from services such as twitter. AndI will say, Wordpress has a slick feature for this. I’m a die hard modxer and I imagine there are many others out there, we should pull together and make this happen, eh?
                ~Shawn Himmelberger
                Check out my MODx Development and MODx Design Company - Himmelberger Design
                • 18373 ☆ A M B ☆
                • 3,141 Posts
                I guess it would be possible to use an uncached snippet as the entire template content that outputs chunkA for regular content and chunkB which is mobile optimized. Something like the way Personalize works.

                Then if you just add the actual chunk (cached) into the snippet call like [[!Mobile? &yes=`[[$mobileTemplate]]` &no=`[[$regularTemplate]]`]] you will still get the caching benefits (it only switches the chunk to use - but the chunk itself is cached), no duplicate content but complete control over the output etc.

                Downside is that it aint too pretty to replace your entire template with a chunk in my opinion, but it would definitely work and even allow different templates per page.

                Triggering the snippet could be based on the user agent, or a slightly modified .htaccess at m.domain.com which sends something like &mobile=1 along with the request.
                  Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                  Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                  • 27830
                  • 3 Posts
                  Hi Guys, Just created a mobile version of a clients website to find that it is having this caching problem. I have unticked cachable from each resource now and it seems to have resolved it but obviously the main site is now running slooooooooow. Has anyone found a step by step way to get ModMobile to work properly?

                  Read all the posts above but couldn't figure out exactly what needed to be done. I found the snippet to replace but got lost on the code below regarding the returned resource and can't see anywhere in setting to change the onload function? Any help would be marvelous.