Jump to:

10994 Posts in 2728 Topics by 1819 members

All other Modules

SilverStripe Forums » All other Modules » mobile module: redirecting to a specific page

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 744 Views
  • Anatol
    Avatar
    126 Posts

    mobile module: redirecting to a specific page Link to this post

    Hi,

    I made a small change to the mobile module because I prefer that links to a specific page should be redirected to the same page under the mobile subdomain, so something like

    http://mydomain.com/link/to/page/ should redirect to
    http://m.mydomain.com/link/to/page/ (not to the default http://m.mydomain.com/)

    Mainly because if I get a link by email or through another page I would end up on the home page, not on the actual page the link should go to.

    It's an easy fix. In MobileSiteControllerExtension, method onBeforeInit add $this->owner->RelativeLink() to both redirect functions.

    return $this->owner->redirect($config->FullSiteDomain . $this->owner->RelativeLink());

    Or, if you also have modules installed that use the 'Action' and 'ID' URL parameters (such as the Image Gallery, e.g. http://mydomain.com/gallery/album/my-holiday ) you could add the code in green:

    /**
        * Override the default behavior to ensure that if this is a mobile device
        * or if they are on the configured mobile domain then they receive the mobile site.
        */
       public function onBeforeInit() {
          $config = SiteConfig::current_site_config();
          
          $URLParams = Director::URLParams();
          if ($this->owner->RelativeLink() != '/') $url_params = $this->owner->RelativeLink();
          if ($URLParams['Action']) $url_params .= $URLParams['Action'].'/';
          if ($URLParams['ID']) $url_params .= $URLParams['ID'];
          if ($URLParams['OtherID']) $url_params .= '/'.$URLParams['OtherID'];

          // Redirect users to the full site if requested (cookie expires in 30 minutes)
          if(isset($_GET['fullSite'])) {
             $_COOKIE['fullSite'] = 1;
             setcookie('fullSite', 1, time() + self::$cookie_expire_time);
          }

          // Redirect to the full site if user requested
          if($this->onMobileDomain() && !empty($_COOKIE['fullSite']) && $config->MobileSiteType == 'RedirectToDomain') {
             return $this->owner->redirect($config->FullSiteDomain . $url_params);
          } elseif(!empty($_COOKIE['fullSite'])) {
             return; // nothing more to be done
          }

          // If the user requested the mobile domain, set the right theme
          if($this->onMobileDomain()) {
             SSViewer::set_theme($config->MobileTheme);
          }

          // User just wants to see a theme, but no redirect occurs
          if(MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'MobileThemeOnly') {
             SSViewer::set_theme($config->MobileTheme);
          }

          // If on a mobile device, but not on the mobile domain and has been setup for redirection
          if(!$this->onMobileDomain() && MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'RedirectToDomain') {
             return $this->owner->redirect($config->MobileDomain . $url_params);
          }
       }

    There may be a more 'pretty' solution, but for me this works.

    Cheers!
    Anatol

    744 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.