Umbraco Controller
Contain or reuse logic across Elements. A Controller enables you to separate logic while still being connected with the life cycle of an element.
Last updated
Was this helpful?
Contain or reuse logic across Elements. A Controller enables you to separate logic while still being connected with the life cycle of an element.
Last updated
Was this helpful?
An Umbraco controller provides all the features of an Umbraco element within a separate class. Do this for architectural reasons or to reuse a feature across elements.
A Controller is assigned to a Host Element. This assignment may be indirect, since Controllers can host other Controllers.
The host element is a web component enhanced to host controllers. All are controller hosts, as are all Umbraco controllers, allowing controllers to host other controllers.
To retrieve the controller’s host element, use the getHostElement()
method.
Controllers can declare the following methods, which are triggered depending on the host element’s life cycle:
hostConnected()
— Called when the host element connects to the DOM.
hostDisconnected()
— Called when the host element disconnects from the DOM.
destroy()
— Called when the controller is taken out of commission.
Additionally, Umbraco Controllers implement a getHostElement()
method, which enables any Controller to receive the Element that hosts the Controllers.
A Controller should register itself with a given host. This is handled automatically when extending the UmbControllerBase
class. The following example demonstrates a controller implementation:
If you don't like to extend the UmbControllerBase
, then you can register a class as a controller as shown in the following example. Note that manual unregistration is required in this case.