用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/umbraco/Umbraco-CMS-Backoffice-Skills --skill umbraco-controllers命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
Run tests from skill examples and generate a report (project)
Validate links and references in SKILL.md files using deterministic scripts
Umbraco backoffice extension customisation - complete working examples showing how extension types combine
| name | umbraco-controllers |
| description | Understand and create controllers in Umbraco backoffice (foundational concept) |
| version | 1.0.0 |
| location | managed |
| allowed-tools | Read, Write, Edit, WebFetch |
Controllers are separate classes that contain or reuse logic across elements while maintaining connection to an element's lifecycle. A Controller is assigned to a Host Element and supports lifecycle methods (hostConnected, hostDisconnected, destroy) for managing side effects, timers, subscriptions, and cleanup. Controllers can host other controllers, enabling composition and reuse of functionality.
Always fetch the latest docs before implementing:
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
export class MyController extends UmbControllerBase {
constructor(host: UmbControllerHost) {
super(host);
// Auto-registers with host
}
override hostConnected() {
super.hostConnected();
console.log('Controller connected!');
}
override hostDisconnected() {
super.hostDisconnected();
console.log('Controller disconnected!');
}
override destroy() {
super.destroy();
console.log('Controller destroyed!');
}
}
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
export class TimerController extends UmbControllerBase {
#timer?: number;
#secondsAlive = 0;
constructor(host: UmbControllerHost) {
super(host);
}
override hostConnected() {
super.hostConnected();
// Start timer when element connects to DOM
this.#timer = window.setInterval(this.#onInterval, 1000);
}
override hostDisconnected() {
super.hostDisconnected();
// Clean up timer when element disconnects
if (this.#timer) {
clearInterval(this.#timer);
}
}
#onInterval = () => {
this.#secondsAlive++;
console.log();
};
() {
.();
(.#timer) {
(.#timer);
}
}
(): {
.#secondsAlive;
}
}
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { TimerController } from './timer-controller.js';
export class MyElement extends UmbLitElement {
#timerController = new TimerController(this);
render() {
return html`
<div>
Active for: ${this.#timerController.getSecondsAlive()}s
</div>
`;
}
}
export class MyManualController {
#host: UmbControllerHost;
constructor(host: UmbControllerHost) {
this.#host = host;
// Manual registration required
this.#host.addUmbController(this);
}
hostConnected() {
console.log('Connected');
}
destroy() {
// Manual deregistration required
this.#host.removeUmbController(this);
}
}
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification';
export class NotificationController extends UmbControllerBase {
async showSuccess(message: string) {
// Controllers can access contexts via getContext
const context = await this.getContext(UMB_NOTIFICATION_CONTEXT);
context?.peek('positive', { data: { message } });
}
}
Host Element: Web component that hosts the controller (all Umbraco Elements can be hosts)
Lifecycle Methods:
hostConnected() - Called when host element connects to DOMhostDisconnected() - Called when host element disconnects from DOMdestroy() - Called when controller is permanently destroyedAuto-Registration: Extending UmbControllerBase automatically registers/deregisters
Controller Composition: Controllers can host other controllers
Context Access: Controllers can consume contexts via getContext() and consumeContext()
Use Cases:
API Calls: When making API calls from controllers, NEVER use raw fetch(). Always use a generated OpenAPI client configured with Umbraco's auth context. See the umbraco-openapi-client skill for setup.
That's it! Always fetch fresh docs, keep examples minimal, generate complete working code.
基于 SOC 职业分类