How To Write a custom Function In ColdFusion?
Description:-
This function is used to get URL
parameters from an existing ColdBox Link by providing the link handler name.
<cfscript>
private string function getUrlParam(
required string handlerName,
required string urlLink
){
var link = "";
var loopCounter = 0;
var indexParam = "";
var urlExtraParamStart = arguments.urlLink.listFind( arguments.handlerName, "/" ) + 1;
cfloop ( array = arguments.urlLink.listToArray( "/" ), index = "indexParam" ){
loopCounter++;
if ( loopCounter > urlExtraParamStart ){
link &= len( link ) > 0 ? "/#indexParam#" : indexParam;
}
}
return link;
}
</cfscript>
Attributes:-
handlerName:-
This is a required attribute, and its type is a string. It is the handler name of the ColdBox link.
urlLink:-
This is a required attribute, and its type is a string. It is a ColdBox link from where the URL
parameter will be fetched.
Example:-
In this example, we will extract the URL
parameter from a ColdBox link.
<cfscript>
writeOutput(
getUrlParam(
urlLink = "//192.168.0.1/testHandler/index/id/42/name/joy/age/30",
handlerName = "testHandler"
)
);
</cfscript>
Result:-
id/42/name/joy/age/30
In this user-defined/custom
function we first check where the Haldler name exists in the URL, plus +1 is used to escape Haldler handlerEntryPoint
and then we convert this string URL link into an array and also run an array loop on it, In this loop we extract the values after handlerEntryPoint
and concatenate it in a string and return it.