0

I am working on a silverstripe subdomains' issue, someone else did the code, so any help would be appreciated.

There is a "About Us" nav bar on the main website eg www.mainwebsite.com, and we would like the "About Us" to disappear for any subdomains eg subdomain.mainwebsite.com.

I can see from the Silverstripe backend, there is a 'Hide in Subdomains' function, and I ticked it. However, the "About Us" link disappears for some time and it comes back at other tomes on both main site and subdomains.

Can anybody point me to the right direction how can I fix this problem? I can copy any code here if you need. Please help.

Thanks heaps, S:)

add comments, I have found this code in my page.php in my site - code folder if it is useful

function ShowMenuInSubdomain()
{
    $host = explode('.',$_SERVER['HTTP_HOST']);
    $subdomain = $host[0];
    if($subdomain != 'www' && $this->HideInSubdomains) {
        return true;    
    }       
}

add comments, here is navigation part in Page.ss in the theme - templates folder I am using

<div id="Header">
    <div id="HeaderWrapper">
        <div id="LogoWrapper"><a href="{$BaseHref}">$GetSubDomainHeaderImage</a></div>
        <div id="Navigation">
            <% cached 'Navigation', Aggregate(Page).Max(LastEdited) %>
            <% include Navigation %>
          <% end_cached %>
        </div>
    </div>
</div>

add comments, here is what my navigation.ss in the templates - includes folder looks like

<ul>
<% control Menu(1) %>
    <% if ShowMenuInSubdomain %>
    <% else %>
    <li <% if Children %>class="hasChildren"<% end_if %>>
    <a href="$Link" title="$Title.XML" class="$LinkingMode">$MenuTitle.XML <% if ShowMenuInSubdomain %>0<% end_if %></a>
        <% if Children %>
        <ul>
        <% control Children %>
            <li <% if Children %>class="hasChildren"<% end_if %>>
            <a href="$Link" title="$Title.XML" class="$LinkingMode">$MenuTitle.XML</a>
            <% if Children %>
            <ul>
            <% control Children %>
            <ul class="thridUL{$Pos}">
                <% if DisableLink %>
                <li>
                <span class="$LinkingMode thirdLevelTitle lookLikeALink">$MenuTitle.XML</span>
                </li>
                <% else_if ShowLabelInMenu==0 %>
                <li>
                <a href="$Link" title="$Title.XML" class="$LinkingMode thirdLevelTitle">$MenuTitle.XML</a>
                </li>
                <% end_if %>
                <% if Children %>
                <% control Children %>
                <li>
                <a href="$Link" title="$Title.XML" class="$LinkingMode">$MenuTitle.XML</a>
                <% if Summary %>
                    <span class="menuSummary">
                        $Summary
                        <span class="menuSummaryThumb">$Thumbnail.PaddedImage(160, 160)</span>
                    </span>
                <% end_if %>
                </li>
                <% end_control %>
                <% end_if %>
            </ul>
            <div class="clear">&nbsp;</div>
            <% end_control %>
            </ul>
            <% end_if %>
            </li>
        <% end_control %>
        </ul>
        <% end_if %>
    </li>

    <% end_if %>
<% end_control %>
<li id="calculatorWrapper">
 <a id="Calculator" href="$distanceCalculator.Link" rel="shadowbox;height=800;width=1000"><span>Journey Planner</span></a>
</li> </ul>

Sorry it is a bit long but any help is appreciated. Thanks.

Hi everyone, it seems to be working now when I deleted <% cached 'Navigation', Aggregate(Page).Max(LastEdited) %> <% end_cached %> in page.ss. Can someone please kindly explain what was this line for or the meaning of it? Thanks.

4

2 回答 2

2

这样做:

$_host = explode('.', $_SERVER['HTTP_HOST']); 
if(count($_host) == 3 && $_host[0] != "www") echo "Hide About Us";
于 2011-05-25T22:03:20.490 回答
1

由于您已经为您找到了 subodmain 检查的方法('ShowMenuInSubdomain'),因此现在查找呈现菜单的模板。对于标准的 silverstripe 安装,这很可能/mysite/templates/Page.ss/mysite/templates/layout.

您的菜单可能会呈现在如下块内:

<ul>
<% control Menu(1) %>  
<li><a href="$Link" title="Go to the $Title page" class="$LinkingMode">$MenuTitle</a></li>
<% end_control %>
</ul>

您只需要包装列表项,然后使用调用您的ShowMenuInSubdomain函数的控制块,如下所示:

<% if ShowMenuInSubdomain %>
<li><a href="$Link" title="Go to the $Title page" class="$LinkingMode">$MenuTitle</a></li>
<% end_if %>

如果您遇到问题,请发布相应的模板代码块。

注意:我认为有一个“!” 在您的 ShowMenuInSubdomain 函数中丢失,因为它当前读取为“如果(HideInSubdomains)则 ShowMenuInSubdomain 为真”,因此有问题的行可能应该是:

if($subdomain != 'www' && !$this->HideInSubdomains) {

(注意 $this->HideInSubdomains 之前的“!”)

编辑

首先,忘记GetSubDomainMenu函数,看起来好像它已被更简洁的解决方案所取代,即使用内置Menu控件和ShowMenuInSubdomain函数进行子域检查。

第二,忘记我关于丢失的'!'的注释 在$this->HideInSubdomains上面之前。从函数的使用方式我可以看到它做了它应该做的事情,函数只是以一种误导的方式命名:ShowMenuInSubdomain应该读取HideMenuInSubdomain。令人困惑,但显然不是问题的根源。

所以,从你到目前为止发布的代码来看,没有明显的错误,所以你应该尝试验证以下内容:a)实际上是'navigation.ss'(不是你发布的.cc)渲染的模板?只需在文件中添加一些测试输出以确保(并将'?flush = 1'添加到您的url以清除模板缓存)b)该ShowMenuInSubdomain函数实际上被调用了吗?让它在第一行返回一些字符串,比如'return "working"',然后添加$ShowMenuInSubdomain到你的模板中

如果您仍然卡住,您可以压缩您的 silverstripe 项目文件夹并将其放在某个地方以供下载(首先删除数据库访问凭据等关键信息!),所以我可以看看。祝你好运!

编辑二 - 解决方案 - 最后:)

看起来好像您发现了有缺陷的代码部分。您删除的行应该缓存导航,因此每次渲染时都不必组装(我想您熟悉“缓存”的概念)。查看有关部分缓存的 silverstripe 文档以获取更多解释。注意那里“聚合”下的第一个代码片段 - 这正是您删除的代码。

但是为什么删除缓存部分可以解决您的问题?事实上,答案很简单:由于您的缓存导航只会在编辑某些页面后更新,因此不会调用子域检查功能,除非您在子域上。有时就这么容易:)

于 2011-05-26T08:04:55.263 回答