Coldfusion - RIP Tutorial

Transcription

coldfusion#coldfusion

Table of ContentsAbout1Chapter 1: Getting started with coldfusion2Remarks2Versions2Examples3Installation or SetupLinux (Ubuntu) InstallationLucee (Open Source)333ColdFusion / CFML Interpretor3Nginx3Adobe (Closed Source)4ColdFusion / CFML Interpretor4Nginx4Hello World5Chapter 2: CFLOOP How-To7Remarks7Examples7Looping through a collection using CFML tags.7Looping through a collection using CFSCRIPT.7Index7Parameters7Basic index loop8Increase step to 28Decrement step by 19CFLoop in a Function9ColdFusion 11 through currentConditionTag syntax101010Parameters10Generated HTML10

CFScript10Previous to ColdFusion 811ColdFusion 8 through current11ColdFusion 11 through current11Generated HTML11Date or time range11Query11Parameters11Example queryTag syntax1212Generated HTML12Limiting output to specific rows13Grouping Output13CFScript14ColdFusion 6 (MX) though current15ColdFusion 8 though current15ColdFusion 10 though current15ColdFusion 11 though current15List16Tag syntax16Parameters16Generated HTML16CFScript16Previous to ColdFusion 816ColdFusion 8 through current17ColdFusion 9 through current17ColdFusion 11 through current17Generated HTML17Array17Tag syntax17ColdFusion 8 through current17

Parameters18Generated HTML18ColdFusion 2016 through current18ParametersGenerated HTMLCFScript181919Previous to ColdFusion 819ColdFusion 8 through current19ColdFusion 9 through current19ColdFusion 11 through current19Generated HTML19File20Structure20Tag syntax20Parameters20Using Structure Functions20Implicit Structure Syntax20Generated HTML21CFScript21Output the structure's keys21Generated HTML21Output the value of the structure's keys21Using Structure Functions21Implicit Structure Syntax21ColdFusion 11 through current22Generated HTML22Index Loop22Conditional Loop22Query Loop22List Loop23File Loop23COM Collection/Structure Loops23

Chapter 3: cfquery24Parameters24Examples24Using cfquery within a Function24Query of Query24Function Calls24User.cfc24Chapter 4: ColdFusion ating Arrays26Creating arrays explicitly using ArrayNew()26History26Declaration26Using ArrayAppend()27Creating 1-D Array Implicitly28Create 2-D Array Implicitly29Array in CFScriptSimilarly, for 2 Dimension Array:General informationChapter 5: Creating REST APIs in coldfusion30313132Introduction32Examples32Creating backend32The interface32Chapter 6: Database QueriesExamples3333Working with databases33Basic Example33

Authentication34Cached Queries34Limiting the Number of Records Returned35Timeouts35Chapter 7: How to invoke a private method dynamically36Remarks36Examples36CFML36CFSCRIPT (CF10 )36Chapter 8: Scopes in Coldfusion37Introduction37Examples37Request Scopes37Global Scopes37Components and functions37Custom tags37Common scopes38Overview38Chapter 9: Variables39Parameters39Examples39Using cfset39Using cfparam39Checking if a Variable Exists40Setting a variable scope40Chapter 10: Working with RegExp Replace ples42User defined REReplaceCallback function43Using REReplaceCallback function43

Credits44

AboutYou can share this PDF with anyone you feel could benefit from it, downloaded the latest versionfrom: coldfusionIt is an unofficial and free coldfusion ebook created for educational purposes. All the content isextracted from Stack Overflow Documentation, which is written by many hardworking individuals atStack Overflow. It is neither affiliated with Stack Overflow nor official coldfusion.The content is released under Creative Commons BY-SA, and the list of contributors to eachchapter are provided in the credits section at the end of this book. Images may be copyright oftheir respective owners unless otherwise specified. All trademarks and registered trademarks arethe property of their respective company owners.Use the content presented in this book at your own risk; it is not guaranteed to be correct noraccurate, please send your feedback and corrections to info@zzzprojects.comhttps://riptutorial.com/1

Chapter 1: Getting started with coldfusionRemarksThis section provides an overview of what coldfusion is, and why a developer might want to use it.It should also mention any large subjects within coldfusion, and link out to the related topics. Sincethe Documentation for coldfusion is new, you may need to create initial versions of those relatedtopics.VersionsVersionRelease DateCold Fusion version 1.01995-07-02Cold Fusion version 1.51996-01-01Cold Fusion version 2.01996-10-01Cold Fusion version 3.01997-06-01Cold Fusion version 3.11998-01-01ColdFusion version 4.01998-11-01ColdFusion version 4.5.11999-11-01ColdFusion version 5.02001-06-01ColdFusion MX version 6.02002-05-01ColdFusion MX version 6.12003-07-01ColdFusion MX 72005-02-07ColdFusion 82007-07-30ColdFusion 92009-10-05ColdFusion 102012-05-15ColdFusion 112014-04-29ColdFusion 20162016-02-16https://riptutorial.com/2

ExamplesInstallation or SetupLinux (Ubuntu) InstallationLucee (Open Source)ColdFusion / CFML InterpretorDownload the appropriate file from their site (http://lucee.org/downloads.html) and execute theirinstallerwget cee-5.0.0.252-pl0-linux-x64installer.runsudo chmod x lucee-5.0.0.252-pl0-linux-x64-installer.runsudo ./lucee-5.0.0.252-pl0-linux-x64-installer.runStep through installer.NginxInstall Nginx on your serversudo apt-get install nginxEdit your /etc/nginx/sites-available/defaultserver {listen 80;server name ;root /opt/lucee/tomcat/webapps/ROOT;index index.cfm index.html index.htm;#Lucee Admin should always proxy to Luceelocation /lucee {include lucee.conf;}#Pretty URLslocation / {try files uri /index.cfm uri? is args args;include lucee.conf;}location \.cfm {include lucee.conf;}https://riptutorial.com/3

location \.cfc {include lucee.conf;}}Edit /etc/nginx/lucee.confproxy pass http://127.0.0.1:8888;proxy set header Host http host;proxy set header X-Real-IP remote addr;proxy set header X-Forwarded-For proxy add x forwarded for;proxy set header X-Forwarded-Proto scheme;Reload nginxsudo service nginx reloadAccess the Lucee Server admin 888/lucee/admin/server.cfmYour root web directory lives here:/opt/lucee/tomcat/webapps/ROOTAdobe (Closed Source)ColdFusion / CFML InterpretorDownload the appropriate file from their site d-trial/try.html) and execute their installerwget URL /ColdFusion 2016 WWEJ linux64.binsudo chmod x ColdFusion 2016 WWEJ linux64.binsudo ./ColdFusion 2016 WWEJ linux64.binStep through installer. Make sure you select the internal web server (port 8500)NginxInstall Nginx on your serversudo apt-get install nginxhttps://riptutorial.com/4

Edit your /etc/nginx/sites-available/defaultserver {listen 80;server name ;root /opt/coldfusion2016/cfusion/wwwroot;index index.cfm index.html index.htm;location / {try files uri uri/ 404;}location /CFIDE/administrator {deny all;}location * \.(cfm cfml cfc html) {include /etc/nginx/conf/dc tomcat connector.conf;}location /rest {include tomcatconf;}}Edit /etc/nginx/tomcat.confproxy pass http://127.0.0.1:8500;proxy set header Host host;proxy set header X-Forwarded-Host host;proxy set header X-Forwarded-Server host;proxy set header X-Forwarded-For http x forwarded for;proxy set header X-Real-IP remote addr;Reload nginxsudo service nginx reloadAccess the Adobe ColdFusion Server admin our root web directory lives here:/opt/coldfusion2016/cfusion/wwwrootHello WorldFile: test.cfmTag Implementationhttps://riptutorial.com/5

cfoutput Hello World! /cfoutput CFScript Implementation cfscript writeOutput("Hello World!"); /cfscript Read Getting started with coldfusion online: ngstarted-with-coldfusionhttps://riptutorial.com/6

Chapter 2: CFLOOP How-ToRemarksBig thanks to Pete Freitag for his CFScript Cheat Sheet Adam Cameron for CF 11: CFLOOP in CFScript is Very Broken (and it still is in CF 2016).ExamplesLooping through a collection using CFML tags. !--- Define collection --- cfset attributes {"name": "Sales","type": "text","value": "Connection"} !--cfloop tag with attribute collection can be used toloop through the elements of a structure--- cfloop collection #attributes# item "attribute" cfoutput Key : #attribute#, Value : #attributes[attribute]# /cfoutput /cfloop Looping through a collection using CFSCRIPT. cfscript /*define collection*/attributes {"name": "Sales","type": "text","value": "Connection"};for(attribute in attributes){/* attribute variable will contain the key name of each key value pair in loop */WriteOutput('Key : ' & attribute & ', Value : ' & attributes[attribute] & ' br/ ');} /cfscript IndexParametershttps://riptutorial.com/7

e name for the loop's index. Defaults tothe variables scope.fromtruenumericStarting value for the index.totruenumericEnding value for the index.stepfalsenumeric1DescriptionValue by which to increase or decrease theindex per iteration.Basic index loopFinal value of x is 10. !--- Tags --- cfoutput cfloop index "x" from "1" to "10" li #x# /li /cfloop /cfoutput !--- cfscript --- cfscript for (x 1; x 10; x ) {writeOutput(' li ' & x & ' /li ');} /cfscript !--- HTML Output --- - 1- 2- 3- 4- 5- 6- 7- 8- 9- 10Increase step to 2Final value of x is 11. !--- Tags --- cfoutput cfloop index "x" from "1" to "10" step "2" li #x# /li /cfloop /cfoutput !--- cfscript --- cfscript for (x 1; x 10; x 2) {https://riptutorial.com/8

writeOutput(' li ' & x & ' /li ');} /cfscript !--- HTML Output --- - 1- 3- 5- 7- 9Decrement step by 1Final value of x is 0. !--- Tags --- cfoutput cfloop index "x" from "10" to "1" step "-1" li #x# /li /cfloop /cfoutput !--- cfscript --- cfscript for (x 10; x 0; x--) {writeOutput(' li ' & x & ' /li ');} /cfscript !--- HTML Output --- - 10- 9- 8- 7- 6- 5- 4- 3- 2- 1CFLoop in a FunctionMake sure to var or local scope the index inside a function. Foo() returns 11. !--- var scope --- cffunction name "foo" access "public" output "false" returntype "numeric" cfset var x 0 / cfloop index "x" from "1" to "10" step "1" cfset x / /cfloop cfreturn x / /cffunction !--- Local scope --- cffunction name "foo" access "public" output "false" returntype "numeric" cfloop index "local.x" from "1" to "10" step "1" cfset local.x / https://riptutorial.com/9

/cfloop cfreturn local.x / /cffunction ColdFusion 11 through currentThe cfscript function cfloop has no support for index as a stand alone countermechanism.ConditionTag stringDefaultDescriptionCondition that manages the loop. Cannot containmath symbols like , or . Must use ColdFusiontext implementations like less than, lt, greater than, gt, equals or eq.Final value of x is 5. cfset x 0 / cfoutput cfloop condition "x LT 5" cfset x / li #x# /li /cfloop /cfoutput Generated HTMLThis will also have a line break between each line of HTML. li 1 /li li 2 /li li 3 /li li 4 /li li 5 /li CFScripthttps://riptutorial.com/10

Previous to ColdFusion 8 cfscript x 0;while (x LT 5) {x x 1;writeOutput(' li ' & x & ' /li ');} /cfscript ColdFusion 8 through current cfscript x 0;while (x LT 5) {x x ;writeOutput(' li ' & x & ' /li ');} /cfscript ColdFusion 11 through currentThe cfscript function cfloop has no support for condition.Generated HTMLNotice that the cfscript output is all on one line. li one /li li two /li li three /li li four /li Date or time rangeExample for date or time range.QueryConsider the table dbo.state zip, which contains the columns city, statecode and zipcode and hasover 80,000 ringThe variable name of a query object.startrowfalsenumericThe starting row index of the query 1

numericThe ending row index of the query object.groupfalsestringThe query column name on which to grouprecords.Example query cfquery name "geo" datasource "reotrans-dev" SELECT city, stateCode, zipCodeFROM dbo.state zip /cfquery Tag syntaxUsing the query object geo as the source for cfloop. Since the table dbo.state zip has so manyrecords, the HTML generated will take quite some time. This example shows only the first 20records' worth of HTML. cfoutput ul cfloop query "geo" !--- Scope the column names with the query name. --- li #geo.city# #geo.stateCode# #geo.zipCode# /li /cfloop /ul /cfoutput Generated HTML ul li 100 PALMS CA 92274 /li li 1000 PALMS CA 92276 /li li 12 MILE IN 46988 /li li 1ST NATIONAL BANK OF OMAHA NE 68197 /li li 29 PALMS CA 92277 /li li 29 PALMS CA 92278 /li li 3 STATE FARM PLAZA IL 61710 /li li 3 STATE FARM PLAZA IL 61791 /li li 30TH STREET PA 19104 /li li 3M CORP MN 55144 /li li 65TH INFANTRY PR 00923 /li li 65TH INFANTRY PR 00924 /li li 65TH INFANTRY PR 00929 /li li 65TH INFANTRY PR 00936 /li li 7 CORNERS VA 22044 /li li 88 KY 42130 /li li 9 MILE POINT LA 70094 /li li A A R P INS PA 19187 /li li A A R P PHARMACY CT 06167 /li https://riptutorial.com/12

li A H MCCOY FEDERAL BLDG MS 39269 /li /ul Limiting output to specific rowsTo limit the query's output to a specific range of rows, specify startrow and endrow. cfloop query "geo" startrow "100" endrow "150" li #geo.city# #geo.stateCode# #geo.zipCode# /li /cfloop Grouping OutputIn the example data, the same state listed multiple times in relation to the multiple cities that areassociated to each state. You can also see the same city listed multiple times in relation to themultiple zip codes associated to each city.Let's group the output by state first. Notice the 2nd instance of cfloop wrapped around the contentthat will be output under the stateCode grouped content. cfoutput ul cfloop query "geo" group "stateCode" !--- Scope the column names with the query name. --- li #geo.stateCode# ul cfloop li #geo.city# #geo.zipCode# /li /cfloop /ul /li /cfloop /ul /cfoutput Generated HTML (extract) from one grouped cfloop tag. ul li AK ul li KONGIGANAK 99545 /li li ADAK 99546 /li li ATKA 99547 /li !-- etc. -- /ul /li li AL ul li ALEX CITY 35010 /li li ALEXANDER CITY 35010 /li li ALEX CITY 35011 /li !-- etc. -- /ul https://riptutorial.com/13

/li !-- etc. -- /ul Finally, let's group the output by stateCode, then by city in order to see all the zipCode entries percity. Notice the 2nd cfloop is now grouped by city and a 3rd cfloop exists to output the zipCodedata. cfoutput ul cfloop query "geo" group "stateCode" li #geo.stateCode# ul cfloop group "city" li #geo.city# ul cfloop li #geo.zipCode# /li /cfloop /ul /li /cfloop /ul /li /cfloop /ul /cfoutput Generated HTML (extract) from two grouped cfloop tags. ul li AK ul li ADAK ul li 99546 /li li 99571 /li /ul /li li AKHIOK ul li 99615 /li /ul /li !--- etc. --- li BARROW ul li 99723 /li li 99759 /li li 99789 /li li 99791 /li /ul /li !--- etc. --- /ul /li !--- stateCodes etc. --- /ul https://riptutorial.com/14

CFScriptColdFusion 6 (MX) though current cfscript for (x 1; x LTE geo.recordcount; x x 1) {writeOutput( ' li ' & geo.city[x] & ' ' &geo.stateCode[x] & ' ' & geo.zipCode[x] & ' /li ');} /cfscript ColdFusion 8 though current cfscript for (x 1; x geo.recordcount; x ) {writeOutput( ' li ' & geo.city[x] & ' ' &geo.stateCode[x] & ' ' & geo.zipCode[x] & ' /li ');} /cfscript ColdFusion 10 though currentWith the FORINsyntax, x is a query row object, not the row index. cfscript for (x in geo) {writeOutput( ' li ' & x.city & ' ' &x.stateCode & ' ' & x.zipCode & ' /li ');} /cfscript ColdFusion 11 though currentColdFusion 11 allows most tags to be written as cfscript. cfscript cfloop(query: geo, startrow: 1, endrow: 2) {writeOutput( ' li ' & geo.city & ' ' &geo.stateCode & ' ' & geo.zipCode & ' /li ');} /cfscript With group. cfscript cfloop(query: geo, group: 'city') {writeOutput( ' li ' & geo.city & ' ul ');cfloop() { // no arguments, just as in the tag syntax.https://riptutorial.com/15

writeOutput(' li ' & geo.zipCode & ' /li ');}writeOutput(' /ul /li ');} /cfscript ListConsider this list: cfset foo "one,two,three,four" / Tag nlisttrueA list object. The variable must be evaluated (wrapped with##)indextrueThe current element of the list. cfoutput cfloop list "#foo#" index "x" li #x# /li /cfloop /cfoutput Generated HTMLThis will also have a line break between each line of HTML. li one /li li two /li li three /li li four /li CFScriptPrevious to ColdFusion 8 cfscript for (x 1; x LTE listLen(foo); x x 1) {writeOutput(" li " & listGetAt(foo, x) & " /li ");https://riptutorial.com/16

} /cfscript ColdFusion 8 through current cfscript for (x 1; x listLen(foo); x ) {writeOutput(" li " & listGetAt(foo, x) & " /li ");} /cfscript ColdFusion 9 through current cfscript for (x in foo) {writeOutput(" li " & x & " /li ");} /cfscript ColdFusion 11 through currentThe cfscript function cfloop has no support for list.Generated HTMLNotice that the cfscript output is all on one line. li one /li li two /li li three /li li four /li ArrayThe ability to directly use an array object with cfloop was added in ColdFusion 8.Consider this array; cfset aFoo ["one", "two", "three", "four"] / Tag syntaxColdFusion 8 through currenthttps://riptutorial.com/17

Using the attribute index by onarraytrueAn array object. The variable must be evaluated (wrappedwith ##)indextrueThe current element of the array. cfoutput cfloop array "#aFoo#" index "x" li #x# /li /cfloop /cfoutput Generated HTMLThis will also have a line break between each line of HTML. li one /li li two /li li three /li li four /li ColdFusion 2016 through currentThe attribute item changes the behavior of cfloop as of Coldfusion 2016.Using the attribute item instead of or in addition to narraytrueAn array object. The variable must be evaluated (wrappedwith ##)itemtrueThe current element of the array.indexfalseThe current index of the array. cfoutput cfloop array "#aFoo#" item "x" index "y" li #x# #y# /li /cfloop /cfoutput https://riptutorial.com/18

Generated HTMLThis will also have a line break between each line of HTML. li one 1 /li li two 2 /li li three 3 /li li four 4 /li CFScriptPrevious to ColdFusion 8 cfscript for (i 1; x LTE arrayLen(aFoo); i i 1) {writeOutput(" li " & aFoo[i] & " /li ");} /cfscript ColdFusion 8 through current cfscript for (i 1; i arrayLen(aFoo); i i ) {writeOutput(" li " & aFoo[i] & " /li ");} /cfscript ColdFusion 9 through currentWith the FORINsyntax, x is the current array element, not the array index. cfscript for (x in aFoo) {writeOutput(" li " & x & " /li ");} /cfscript ColdFusion 11 through currentThe cfscript function cfloop has no support for array.Generated HTMLNotice that the cfscript output is all on one line.https://riptutorial.com/19

li one /li li two /li li three /li li four /li File cfloop list "#myFile#" index "FileItem" delimiters "#chr(10)##chr(13)#" cfoutput #FileItem# br / /cfoutput /cfloop StructureConsider this structure: cfset stFoo {a "one", b "two", c "three", d "foue"} / Tag syntaxParametersNotice the use of the attribute item instead of ctiontruestructureA struct object. The variable must be evaluated(wrapped with ##).itemtruestringThe current structure key,Using Structure Functions cfoutput cfloop collection "#

ColdFusion version 4.0 1998-11-01 ColdFusion version 4.5.1 1999-11-01 ColdFusion version 5.0 2001-06-01 ColdFusion MX version 6.0 2002-05-01 ColdFusion MX version 6.1 2003-07-01 ColdFusion MX 7 2005-02-07 ColdFusion 8 2007-07-30 ColdFusion 9 2009-10-05 ColdFusion 10 2012-05-15 ColdFusion 11 2014-04