Global Helpers
service()
Signature
Section titled “Signature”service() — returns any
Available in: controller, model, mapper, migrator, migration, tabledefinition
Category: Miscellaneous Functions
Description
Section titled “Description”Resolve a DI-registered service by name.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | yes | — | The registered service name to resolve. |
Examples
Section titled “Examples”// 1. Resolve a registered service and call a method on it
mailer = service("MailerService");
mailer.send(to="user@example.com", subject="Welcome!");
// 2. Resolve a payment gateway service and process a charge
gateway = service("PaymentGateway");
result = gateway.charge(amount=params.amount, token=params.stripeToken);
// 3. Use a service in a model callback to send a notification
component extends="Model" {
function config() {
afterCreate(method="notifyAdmin");
}
private function notifyAdmin() {
notifier = service("NotificationService");
notifier.notify(event="userCreated", userId=this.id);
}
}