Apex Server-Side Controller Overview
Create a server-side controller in Apex and use the @AuraEnabled annotation to enable access to the controller method.
Only methods that you have explicitly annotated with @AuraEnabled are exposed. Calling server-side actions aren’t counted against your org’s API limits. However, your server-side controller actions are written in Apex, and as such are subject to all the usual Apex limits. Apex limits are applied per action.
This Apex controller contains a serverEcho action that prepends a string to the value passed in.
public with sharing class SimpleServerSideController {
//Use @AuraEnabled to enable client- and server-side access to the method
@AuraEnabled
public static String serverEcho(String firstName) {
return ('Hello from the server, ' + firstName);
}
}
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 aren’t supported.
- If a method returns an object, instance methods that retrieve the value of the object’s instance field must be public.
- Use unique names for client-side and server-side actions in a component. A JavaScript function (client-side action) with the same name as an Apex method (server-side action ) can lead to hard-to-debug issues. In debug mode, the framework logs a browser console warning about the clashing client-side and server-side action names.
For more information, see Classes in the Apex Developer Guide.