SilverStripe虚拟页/菜单项
问题描述:
想象一个网站结构/菜单布局是这样的:SilverStripe虚拟页/菜单项
首页
关于我们
服务
__Peeling土豆
__Slicing土豆
__Baking土豆
所有菜单项都链接到一个真实页面,并带有自己的URL和内容。但粗体项目只是一个没有链接,内容和URL的菜单项,它的唯一目的是折叠悬停的子菜单。 SilverStripe不允许您创建这样的Page实体。
我在寻找最清洁,最简单和最哈克的方式仅仅创建一个虚拟页面作为一个菜单项,没有内容,并在最好的情况下也没有一个URL蛞蝓(后者可能是难)。
答
只需创建一个RedirectorPage
并选择第一个子页面作为重定向目标,即可在没有任何附加代码的情况下实现“虚拟”页面。
就我个人而言,过去我使用了一个更简单的“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蛞蝓实际上是有益的,因为你的网址,于是将services/peeling-potatoes
等,这是最有可能更好的搜索引擎优化的目的。