Version: current

      1 <?php
      2
      3 /**
      4  * @package sapphire
      5  * @subpackage control
      6  */
      7 class RootURLController extends Controller {
      8
      9     /**
     10      * @var bool
     11      */
     12     protected static $is_at_root = false;
     13     /**
     14      * @var string
     15      */
     16     protected static $default_homepage_link = 'home';
     17     /**
     18      * @var string
     19      */
     20     protected static $cached_homepage_link;
     21
     22     /**
     23      * Get the full form (e.g. /home/) relative link to the home page for the current HTTP_HOST value. Note that the
     24      * link is trimmed of leading and trailing slashes before returning to ensure consistency.
     25      *
     26      * @return string
     27      */
     28     public static function get_homepage_link() {
     29         if (!self::$cached_homepage_link) {
     30             $host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
     31             $SQL_host = Convert::raw2sql($host);
     32             $candidates = DataObject::get('SiteTree', "\"HomepageForDomain\" LIKE '%$SQL_host%'");
     33
     34             if ($candidates)
     35                 foreach ($candidates as $candidate) {
     36                     if (preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
     37                         self::$cached_homepage_link = trim($candidate->RelativeLink(true), '/');
     38                     }
     39                 }
     40
     41             if (!self::$cached_homepage_link) {
     42                 if (
     43                         Object::has_extension('SiteTree', 'Translatable')
     44                         && $link = Translatable::get_homepage_link_by_locale(Translatable::get_current_locale())
     45                 ) {
     46                     self::$cached_homepage_link = $link;
     47                 } else {
     48                     self::$cached_homepage_link = self::get_default_homepage_link();
     49                 }
     50             }
     51
     52             /* subsite_mod */
     53             if (singleton('SiteTree')->hasExtension('SiteTreeSubsites')) {
     54                 if ($subsite = Subsite::currentSubsite()) {
     55                     $link = SiteTreeSubsites::get_homepage_urlsegment_by_subsite($subsite->ID);
     56                     self::$cached_homepage_link = ($link) ? $link : $subsite->getPrimaryDomain() . '/' .self::get_default_homepage_link();
     57                 }
     58             }
     59             /* end_subsite_mod */
     60         }
     61
     62         return self::$cached_homepage_link;
     63     }
     64
     65     /**
     66      * Gets the link that denotes the homepage if there is not one explicitly defined for this HTTP_HOST value.
     67      *
     68      * @return string
     69      */
     70     public static function get_default_homepage_link() {
     71         return self::$default_homepage_link;
     72     }
     73
     74     /**
     75      * Returns TRUE if a request to a certain page should be redirected to the site root (i.e. if the page acts as the
     76      * home page).
     77      *
     78      * @param SiteTree $page
     79      * @return bool
     80      */
     81     public static function should_be_on_root(SiteTree $page) {
     82         if (!self::$is_at_root && self::get_homepage_link() == trim($page->RelativeLink(true), '/')) {
     83             return!(
     84             $page->hasExtension('Translatable') && $page->Locale && $page->Locale != Translatable::default_locale()
     85             );
     86         }
     87
     88         return false;
     89     }
     90
     91     /**
     92      * Resets the cached homepage link value - useful for testing.
     93      */
     94     public static function reset() {
     95         self::$cached_homepage_link = null;
     96     }
     97
     98     /**
     99      * @param SS_HTTPRequest $request
    100      * @return SS_HTTPResponse
    101      */
    102     public function handleRequest(SS_HTTPRequest $request) {
    103         self::$is_at_root = true;
    104
    105         $this->pushCurrent();
    106         $this->init();
    107
    108         if (!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
    109             $this->response = new SS_HTTPResponse();
    110             $this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
    111             return $this->response;
    112         }
    113
    114         $request = new SS_HTTPRequest($request->httpMethod(), self::get_homepage_link() . '/', $request->getVars(), $request->postVars()
    115         );
    116         $request->match('$URLSegment//$Action', true);
    117
    118         $controller = new ModelAsController();
    119         $result     = $controller->handleRequest($request);
    120
    121         $this->popCurrent();
    122         return $result;
    123     }
    124
    125 }
    126

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -