You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Silverstripe CMS:后台自定义Logo、退出操作及菜单头部修改

Silverstripe后台菜单头部定制(基于ModelAdmin扩展)

Silverstripe后台的顶部Logo依赖模板渲染,要替换它需扩展ModelAdmin的模板并配合自定义配置:

  1. 创建/修改模板文件,路径为templates/SilverStripe/Admin/LeftMenu.ss,找到原Logo渲染代码替换为自定义内容:

    <div class="left-menu__header">
        <a href="$BaseURL" class="left-menu__logo">
            <img src="$CustomLogoURL" alt="$SiteConfig.Title" />
        </a>
    </div>
    
  2. 在你的CustomAdmin类中添加配置与方法,传递自定义Logo的URL:

    <?php
    use SilverStripe\Admin\ModelAdmin;
    
    class CustomAdmin extends ModelAdmin
    {
        private static $url_segment = 'myprofile';
        private static $menu_title = 'My Product Admin';
        private static $menu_icon = 'themes/mddsz/images/icon_home.svg';
        // 自定义Logo路径配置
        private static $custom_logo_path = 'themes/mddsz/images/custom-admin-logo.svg';
    
        public function getCustomLogoURL()
        {
            return $this->config()->get('custom_logo_path');
        }
    
        public function init()
        {
            parent::init();
            // 确保自定义模板优先级
            $this->setTemplate('SilverStripe\\Admin\\LeftMenu');
        }
    }
    
  3. 清除Silverstripe缓存后,后台即可显示替换后的Logo。

二、编辑「我的资料」旁的退出操作

退出操作属于后台用户菜单,提供两种实现方式:

方式1:JS动态修改(快速直接)

在CustomAdmin的init方法中注入JS代码,修改退出按钮的文本或链接:

public function init()
{
    parent::init();
    // 注入自定义JS调整退出操作
    $this->getResponse()->addCustomScript("
        document.addEventListener('DOMContentLoaded', function() {
            const logoutBtn = document.querySelector('.left-menu__user-menu-item--logout a');
            if (logoutBtn) {
                // 修改退出按钮文本
                logoutBtn.textContent = '安全退出';
                // 如需修改跳转链接,取消注释下方代码
                // logoutBtn.href = '/custom-logout-path';
            }
        });
    ");
}

方式2:扩展LeftMenu类(原生全局修改)

创建扩展类LeftMenuExtension.php:

<?php
use SilverStripe\Core\Extension;

class LeftMenuExtension extends Extension
{
    public function updateUserMenuItems(&$items)
    {
        // 遍历用户菜单找到退出项并修改
        foreach ($items as &$item) {
            if ($item['Link'] === $this->owner->Link('logout')) {
                $item['Title'] = '安全退出';
                // 如需修改链接,替换下方URL
                // $item['Link'] = '/custom-logout-path';
                break;
            }
        }
    }
}

在config.yml中注册该扩展:

SilverStripe\Admin\LeftMenu:
  extensions:
    - LeftMenuExtension

注:方式1仅影响当前ModelAdmin对应的后台页面,方式2会全局修改所有后台的退出按钮,按需选择即可。

内容的提问来源于stack exchange,提问作者user805528

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 22:45:21