Something like this ought to do the trick if you have access to the Apex Pages, otherwise there is no way to accomplish this on Salesforce. Start with a base class for your controllers.
public virtual with sharing class BaseController {
public PageReference load()
{
PageReference redirect = null;
// Perform generic page load logic?
Agreement__c agreement = Agreement__c.getInstance();
if(agreement.Agree__c == false)
{
// Create URL to agreement page and pass the page to return to
redirect = Page.agreement;
redirect.getParameters().put('retUrl', ApexPages.currentPage().getUrl());
return redirect;
}
// Allow the page controller to perform any of its own logic
return onLoad();
}
/**
* Classes override this to implement their own page load logic
**/
public virtual PageReference onLoad()
{
return null;
}
}
You can then either use this controller base class as a base class for existing controllers, note the means to have them still provide their own on load logic if needed. Otherwise you can simply reference the base class itself on the page. The additional requirement regardless is for the page developer to include the action attribute.
A page that does not have its own controller load logic...
<apex:page standardController="Test__c" extensions="MyPageController" action="{!load}">
<apex:pageMessages />
</apex:page>
public with sharing class MyPageController extends BaseController {
public MyPageController(ApexPages.StandardController stdControlelr)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Page with controller witn no custom load logic.'));
}
}
A page and controller that has its own on load logic...
<apex:page standardController="Test__c" extensions="MyPageCustomLoadController" action="{!load}">
<apex:pageMessages />
</apex:page>
public with sharing class MyPageCustomLoadController extends BaseController {
public MyPageCustomLoadController(ApexPages.StandardController stdControlelr)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Page with controller and custom load logic.'));
}
public override PageReference onLoad()
{
// Do something interesting here!
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Custom page load called.'));
return null;
}
}
Finally a page that does not have its own controller and simply uses the base one....
<apex:page controller="BaseController" action="{!load}">
<apex:pageMessage title="Page with no controller" severity="Info"/>
</apex:page>
You can also use the above without the standardController attribute and use the base class with custom controllers.
Hope this helps a little!