Silverstripe CMS:后台自定义Logo、退出操作及菜单头部修改
Silverstripe后台菜单头部定制(基于ModelAdmin扩展)
一、替换左侧折叠菜单顶部Logo
Silverstripe后台的顶部Logo依赖模板渲染,要替换它需扩展ModelAdmin的模板并配合自定义配置:
创建/修改模板文件,路径为
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>在你的
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'); } }清除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
相关产品推荐
相关产品推荐

