I’ve gotten into the habit of writing event handlers that can support multiple entry points such as a normal page request and an API/REST request. For a event that validates a form I redirect the user back to the form if errors exist or to the next step if everything went as expected. To allow the same handler to support browser and API/Proxy requests, I have added a UDF to my includes/helpers file that wraps all setNextEvent calls so it can abort if the proxy flag has been raised.
I was curious if anyone else has been doing this same type of handler reuse and if so, how are you handling redirects? Do you think I should decorate the ColdBox Controller?
public void function validate (required Event)
{
var rc = Event.getCollection();
var prc = Event.getCollection(private=true);
// If false, errors will now exist within the User
local.result = doValidationLogic(Event=arguments.Event);
prc.package = (local.result)
? { statuscode = 200, body = { URI = ‘confirm’ }}
: { statuscode = 400
,body = {
errors = rc.User.getMemento(target=‘errors’)
,URI = ‘’
}
};
local.args = (local.result)
? { event=’’, context=arguments.Event }
: { event=‘confirm’,context=arguments.Event };
setRelocation(argumentCollection=local.args);
return;
}
// And this is the logic within the setRelocation UDF
// Do not allow redirects on proxy/api events
if (arguments.context.isAPIRequest())
{
return;
}
// If no event is passed, relocate to the homepage
if (structKeyExists(arguments, ‘event’))
{
if (len(arguments.event) == 0)
{
structDelete (arguments,‘event’);
arguments.URI = ‘/’;
}
}
// If no SSL flag is passed, default to the application setting
if (!structKeyExists(arguments, ‘ssl’))
{
arguments.ssl = getSetting(‘allowSSL’);
}
setNextEvent(argumentCollection=arguments);
Thanks.
Aaron Greenlee
http://aarongreenlee.com/