fromholdio/silverstripe-urlsegmenter

为任何 SilverStripe 数据对象添加 URLSegment 扩展并管理 slug 冲突

安装次数: 1,112

依赖者: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 2

开放问题: 0

类型:silverstripe-vendormodule

3.1.0 2023-05-18 03:22 UTC

This package is auto-updated.

Last update: 2024-09-18 06:17:36 UTC


README

为任何 SilverStripe 数据对象添加 URLSegment 扩展并管理 slug 冲突。

也就是说,它执行以下基本操作:

  • URLSegment 字段(类型为 DBVarchar)添加到您的对象中
  • 提供可选的更新对象 CMS 字段,插入 URLSegment 字段(通过配置 yml 启用此功能)
  • 根据对象标题字段自动生成 URLSegment

需求

SilverStripe 4 或 5

安装

composer require fromholdio/silverstripe-urlsegmenter

从 1.x/2.x 升级到 3.x

存在新的配置变量,它们保持相同的默认效果,但是如果您在项目中覆盖了它们,您需要更新到新的变量名称

private static $urlsegmenter_force_title = true;
private static $urlsegmenter_enable_field = false;

现在通过以下方式切换:

private static $urlsegmenter_forced = true;
private static $urlsegmenter_cmsfield_enabled = false;

使用示例

一旦将扩展应用到数据对象上,它就变成即插即用 - 唯一的例外是。

为了允许扩展自动生成 slug 并管理冲突,您需要告诉扩展您设置的 URLSegment 的集合范围。

class Widget extends DataObject
{
    // or apply via config.yml
    private static $extensions = [
        URLSegmenter::class
    ];
    
    private static $db = [
        'Title' => 'Varchar'
    ];

    private static $has_one = [
        'WidgetCategory' => WidgetCategory::class
    ];
    
    public function getURLSegmenterScope()
    {
        return self::get()
            ->filter('WidgetCategoryID' => $this->WidgetCategoryID)
            ->exclude('ID', $this->ID);
    }
}