1 <?php
2
3 4 5 6
7 class RootURLController extends Controller {
8
9 10 11
12 protected static $is_at_root = false;
13 14 15
16 protected static $default_homepage_link = 'home';
17 18 19
20 protected static $cached_homepage_link;
21
22 23 24 25 26 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
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
60 }
61
62 return self::$cached_homepage_link;
63 }
64
65 66 67 68 69
70 public static function get_default_homepage_link() {
71 return self::$default_homepage_link;
72 }
73
74 75 76 77 78 79 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 93
94 public static function reset() {
95 self::$cached_homepage_link = null;
96 }
97
98 99 100 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.
-