在 SilverStripe 中显示父页面的内容

Display the content of parent page in SilverStripe

我在 SilverStripe 中有简单的树结构。

父页面和子页面。

在 ParentPage 上,我显示了 ChildPages 列表。但我不想让无障碍 url:

/parent-page/child-page

我只想允许

/父页面

这就是我在 ChildPage 索引操作中重定向到 ParentPage 的原因:

class ChildPage_Controller extends Page_Controller
{

    public function index()
    {
        $this->redirect('/parent-page/');
    }

}

它在前端运行良好。但是在 CMS 中,在我单击树中的 ChidPage 后,它会重定向我编辑 ParentPage。 (不是前端 url 而是 admin/pages/edit/show/ParentPageID )。这仅在拆分或预览模式下发生。

请问如何预防?或者还有其他选择吗?谢谢!

init 函数中,您可以检查用户是否有权查看 CMS 以及页面当前是否具有 stage get 变量集:

class ChildPage_Controller extends Page_Controller {

    public function init() {
        parent::init();
        if (!($this->getRequest()->getVar('stage') && Permission::check('VIEW_CMS'))) {
            return $this->redirect($this->Parent()->Link(), 301);
        }
    }

}

这样,普通用户将被重定向,未查看设置了 ?stage=... 的页面的 CMS 用户将被重定向。预览/拆分窗格始终设置 stage get 变量,因此在预览/拆分窗格中查看时页面永远不会被重定向。

所以我终于找到了解决方案。

预览窗格显示在 iframe 中。和父 CMS 框架绑定以更改预览窗格的 url。这是因为当您在预览窗格中单击某些 link 时,CMS 会引导您编辑该页面。因此,当我从 ChildPage 重定向到 ParentPage 时,CMS 窗格会捕获 url 的更改,并且编辑器会重定向到 ParentPage。

我还没有找到方法,如何防止它,最后我需要编辑 CMS 的 javascript(LeftAndMain.Preview.js 第 251 行)。

iframe.bind('load', function() {

                self._adjustIframeForPreview();

                // Load edit view for new page, but only if the preview is activated at the moment.
                // This avoids e.g. force-redirections of the edit view on RedirectorPage instances.


                // self._loadCurrentPage();

                $(this).removeClass('loading');
            });

所以我注释掉了这一行"self._loadCurrentPage();"

评论里写着"This avoids e.g. force-redirections of the edit view on RedirectorPage instances."但是还是重定向了。

也许有更好的方法,但我还没有找到。