Newer Version Available

This content describes an older version of this product. View Latest

Apex Server-Side Controller Overview

Create a server-side controller in Apex and use the @AuraEnabled annotation to enable client- and server-side access to the controller method.
Only methods that you have explicitly annotated with @AuraEnabled are exposed.

Don't store component state in your controller. Store it in a component's attribute instead.

Tip

This Apex controller contains a serverEcho action that prepends a string to the value passed in.

1public with sharing class SimpleServerSideController {
2
3    //Use @AuraEnabled to enable client- and server-side access to the method
4    @AuraEnabled
5    public static String serverEcho(String firstName) {
6        return ('Hello from the server, ' + firstName);
7    }
8}
In addition to using the @AuraEnabled annotation, your Apex controller must follow these requirements.
  • Methods must be static and marked public or global. Non-static methods are not supported.
  • If a method returns an object, instance methods that retrieve the value of the object’s instance field must be public.

For more information, see Understanding Classes in the Apex Code Developer's Guide.