SilverStripe 虚拟页面/菜单项

SilverStripe dummy page / menu item

想象一下这样的网站结构/菜单布局:

首页
关于我们
服务
__Peeling土豆
__Slicing 土豆
__Baking土豆

所有菜单项 link 到一个真实的页面,有自己的 URL 和内容。但是粗体项目只是一个没有 link、内容和 URL 的菜单项,它的唯一目的是在悬停时展开子菜单。 SilverStripe 不允许您创建这样一个开箱即用的页面实体。

我正在寻找最干净、最简单、最简单的方法来创建一个仅用作菜单项的虚拟页面,没有内容,在最好的情况下也是如此没有 URL 弹头(后者可能很难)。

您无需任何额外代码即可实现 "dummy" 页面,只需创建一个 RedirectorPage 和 select 您的第一个子页面作为重定向目标。

就我个人而言,我过去使用过更简单的 "RedirectorPage" 版本,如果直接访问它,它会自动重定向到第一个子页面。

示例:

class ChildRedirectorPage extends Page 
{
    private static $description = 'Page that has no content but automatically redirects to the first of its child-pages';

    public function ContentSource() {
        if($firstChild = $this->Children()->First()) {
            return $firstChild;
        } else {
            return $this;
        }       
    }

    public function Link($action = null) {
        // prevent link "redirection" when we're in CMS
        if (!is_subclass_of(Controller::curr(),'LeftAndMain')){
            if($firstChild = $this->Children()->First()) return $firstChild->Link($action);
            else return parent::Link($action);
        }
    }

    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->removeByName('Content', true);
        return $fields;
    }
}

class ChildRedirectorPage_Controller extends Page_Controller 
{
    function init() {
        parent::init();
        if($firstChild = $this->Children()->First()) {
            $this->redirect($firstChild->Link(), 301);
        }           
    }
}

我认为 URL slug 实际上是有益的,因为你的 URL 将是 services/peeling-potatoes,等等,这很可能更适合 SEO 目的。