Can any one explain the usage of controller and extensions in Visualforce? In which scenario we have to use controller and extensions?
4 Answers
In the context of visualforce, to take advantage of the built-in features of Visualforce to access your org's data, you must use a controller. All controllers are Apex classes, either standard Apex classes (ApexPages.StandardController or ApexPages.StandardSetController) or your own custom Apex class. You can also extend functionality of any of these with an Extension class.
I tend to be a bit picky with my terminology here. Controllers are controllers. Extensions are not controllers. And for me, the difference is that extensions cannot live by themselves on a page. In other words, I'm not permitted to use extensions, unless my page has a controller in the first place.
Another distinguishing feature is that a page is allowed only one controller ever. But I can add as many extensions as I need.
So let me begin (what has become a dissertation) with controllers:
Controllers are attached to your page using one of three mutually exclusive formats, standard controller, standard list/set controller, custom controller.
For a page with a single record, accessing standard data and behavior of your object you would use standard controller:
<apex:page standardController="Account"...> ... </apex:page>
If you want a page with many records, using standard list view, and list actions you could use standard list controller:
<apex:page standardController="Account" recordsetVar="accountsList"...> ... </apex:page>
If you want to use completely custom functionality and don't really need most of the standard object behaviors, custom controller is a good option, but bear in mind, any standard actions you want to use will have to be implemented in your custom class. A page with a custom controller would use the following attribute:
<apex:page controller="MyApexClass"...> ... </apex:page>
But bear in mind, you yourself can instantiate ApexPages.StandardController to use its features and functionality. I often use the pattern below to instantiate standard controller to get access to its methods:
public with sharing class MyApexClass {
private ApexPages.StandardController ctrl;
public Account acct {get;set;}
public MyApexClass(){
try{
acct = [select Name from Account
where Id = : ApexPages.currentPage().getParameters().get('id')];
} catch (QueryException e) {
acct = new Account();
}
ctrl = new ApexPages.StandardController(acct);
}
public PageReference save(){
//here I can put additional functionality or just leave it as-is.
return ctrl.save();
}
}
The above instantiates Standard Controller privately, but then surfaces a public save method that is just a pass-through of the controller's save method. The constructor does the housekeeping of creating the actual record I'm working with.
If I want to add to the functionality of any controller, I can then use an extension. Extensions work with either standard or custom controllers. Standard controller with custom extension, for me, is the most common construct I use for Visualforce that will operate inside my org. I get all the power of the standard controller, but all the flexibility of Apex. The syntax for an extension with a standard controller page would be as follows:
<apex:page standardController="account" extensions="MyExtClass"...> ... </apex:page>
The extension class is then required to implement a constructor that accepts the type of the controller. You will then typically want to get a handle to the standard controller instance that is floating around, and the sObject instance that it represents. So for a standard controller page:
public with sharing class MyExtClass {
private ApexPages.StandardController ctrl;
public Account acct {get;set;}
public MyExtClass(ApexPages.StandardController controllerParam){
ctrl = controllerParam;
acct = (Account) ctrl.getRecord();
}
public PageReference customsave(){
//here I override my standard save functionality before I return the save call.
return ctrl.save();
}
}
With a custom controller and extension, you can probably extrapolate from the above, but just to close the loop.
<apex:page controller="MyApexClass" extensions="MyExtClass"...> ... </apex:page>
Again, the extension class implements a constructor that accepts the type of the controller, only now it is my custom class. From there, apart from getting a local handle to your custom controller instance, there isn't a pattern, per-se, but the local handle allows you to interact directly with your custom controller, in whatever way necessary, of course. :
public with sharing class MyExtClass {
private MyApexClass ctrl;
public MyExtClass(MyApexClass controllerParam){
ctrl = controllerParam;
}
}
There are additional tricks that are very powerful, such as instantiating ApexPages.StandardSetController in your class to take advantage of that class' built-in paging, features. For more on that, there is a great video from Dreamforce 11 that shows how this can work: http://youtu.be/Js00YUpJAjs
There are 3 types of controller (actually 4 but two are variants of the same, standard controller):
- Standard - these are provided by the platform so you can produce Visualforce pages without writing code. You'd use these when you have a singe object to manipulate. It provides a save method to allow you to persist changes. There's a variant of this that handles a collection of records - the standard list controller.
- Custom controller - this is written in Apex and requires you to write code for any behaviour you ned. You'd use these when your page isn't dealing with a main object - e.g. A launch pad that can take you to a number of different sub pages.
- Extension controller. This provides additional functionality to a controller - either a standard controller (e.g to manipulate child records along with a parent) or a custom controller (this is often overlooked and is a way to provide common functionality across a number of pages).
There's more information in the developer's guide at : http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_std.htm
Apex extension is class that extend the the functionality of custom / standard controller
refer to below document for more information:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_def.htm
Standard controllers can often provide all of the functionality and data that you need for your Visualforce page.
However you may need to start coding your own controller code if you want
Custom controller
- Override existing functionality
- Make new actions available to the page
- Customize the navigation
- Use HTTP callouts or web services
- Have greater control of how information is accessed on the page
- You may need wizards
Controller Extension
If you want to keep most of the functionality of a standard or custom controller it is wiser to have extension
controller extension can extend standard or custom controller
-
1The list sounds as if you can't do 1-5 in an extension controller. And "wizard-like behavior" is IMHO related to pages sharing same controller + subset of extensions (that would transfer viewstate across them) and not really a capability of custom controllers. Commented Nov 5, 2012 at 7:49
-
@eyescream You may like to know that this i referred from the standard DEV-501 (student Guide)material of salesforce.Following five points are clearly mentioned as for custom controller. Commented Nov 5, 2012 at 7:55
-
2salesforce.com/us/developer/docs/pages/… & salesforce.com/us/developer/docs/pages/Content/… don't say such thing... I'll be available for chat session in 1-1.5h if you'd like to discuss :) Commented Nov 5, 2012 at 8:11