如何在 Silverstripe 4 中设置和锁定 SiteTree?

How to set & lock SiteTree in Silverstripe 4?

我们正在开展一个项目,其中 Silverstripe 充当具有单个 API 数据点的无头 CMS。

内容全部来自非 SiteTree 数据对象,API 数据点是我们唯一需要的 SiteTree 记录。

确保 API 数据点存在的最佳方法是什么,它是每个 /dev/build 上唯一的 SiteTree 记录。然后我们可以在 LeftAndMain 中禁用 PagesAdmin,这样用户就无法编辑/破坏它。

found code here 对于我们的案例来说看起来有点像这样:

$api = new API/AccessPoint();
$api->Title = 'API';
$api->URLSegment = 'home';
$api->ShowInMenus = '1';
$api->Version = '1';
$api->ParentID = '0';
$api->write();
$api->doRestoreToStage();

但我不确定确保 dev\build 删除所有其他页面并创建此页面的 1 条记录的正确方法。

如有任何帮助,我们将不胜感激。

您可以在 DataObject 中使用 requireDefaultRecords() 在 dev/build 上创建记录。不过,在创建新的之前,您需要进行某种检查,例如

public function requireDefaultRecords()
{
    parent::requireDefaultRecords();

    if (AccessPoint::get()->filter('URLSegment', 'home')->exists()) {
        return;
    }

    // create your new object here
}