Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
This guide has been designed to get you started with ColdBox in fewer than 60 minutes. We will take you by the hand and help you build a RESTFul application in 60 minutes or fewer. After you complete this guide, we encourage you to move on to the Getting Started Guide and then to the other guides in this book.
Please make sure you download and install the latest CommandBox CLI. We will show you how in the Installing ColdBox section.
Grab a cup of coffee
Get comfortable
Now let's create our first controller, which in ColdBox is called Event Handler. Let's go to CommandBox again:
This will generate the following files:
A new handler called hello.cfc
inside of the handlers
folder
A view called index.cfm
in the views/hello
folder
An integration test at tests/specs/integration/helloTest.cfc
.
Now go to your browser and the following URL to execute the generated event:
You will now see a big hello.index
outputted to the screen. You have now created your first handler and view combination.
Let's check out the handler code:
As you can see, a handler is a simple CFC with functions on them. Each function maps to an action that is executed via the URL. The default action in ColdBox is index()
which receives three arguments:
event
- An object that models and is used to work with the current request
rc
- A struct that contains both URL/FORM
variables (unsafe data)
prc
- A secondary struct that is private only settable from within your application (safe data)
The event object is used for many things, in the case of this function we are calling a setView()
method which tells the framework what view to render to the user once execution of the action terminates.
Tip: The view is not rendered in line 7, but rendered after the execution of the action by the framework.
Did you detect a convention here?
The sections in the URL are the same as the name of the event handler CFC (hello.cfc
) and method that was generated index()
. By convention, this is how you execute events in ColdBox by leveraging the following URL pattern that matches the name of a handler and action function.
You can also nest handlers into folders and you can also pass the name of the folder(s) as well.
If no action
is defined in the URL then the default action of index
will be used.
All of this URL magic happens thanks to the URL mappings capabilities in ColdBox. By convention, you can write beautiful URLs that are RESTFul and by convention. You can also extend them and create more expressive URL Mappings by leveraging the config/Router.cfc
which is your application router.
Tip: Please see the event handlers guide for more in-depth information.
Now let's create a virtual event, which is basically just a view we want to execute with no event handler controller needed. This is a great way to incorporate non-mvc files into ColdBox, baby steps!
Open the view now (/views/virtual/hello.cfm
) and add the following:
Then go execute the virtual event:
You will get the Hello From ColdBox Land!
displayed! This is a great way to create tests or even bring in legacy/procedural templates into an MVC framework.
Tip: You can see our layouts and views section for more in-depth information.
Welcome to the world of ColdBox!
We are excited you are taking this development journey with us. Before we get started with ColdBox let's install CommandBox CLI, which will allow you to install/uninstall dependencies, start servers, have a REPL tool and much more.
ColdBox has the following supported IDE Tools:
Sublime -
VSCode -
CFBuilder -
Note : However, you can use your own ColdFusion server setup as you see fit. We use CommandBox as everything is scriptable and fast!
No Java Runtime (30mb)
Embedded Runtime (80mb)
Once you download and expand CommandBox you will have the box.exe
or box
binary, which you can place in your Windows Path or *Unix /usr/bin
folder to have it available system wide. Then just open the binary and CommandBox will unpack itself your user's directory: {User}/.CommandBox
. This happens only once and the next thing you know, you are in the CommandBox interactive shell!
We will be able to execute a-la-carte commands from our command line or go into the interactive shell for multiple commands. We recommend the interactive shell as it is faster and can remain open in your project root.
All examples in this book are based on the fact of having an interactive shell open.
To get started open the CommandBox binary or enter the shell by typing box
in your terminal or console. Then let's create a new folder and install ColdBox into a directory.
You can also install the latest bleeding edge version by using the coldbox@be
slug instead, or any previous version.
To uninstall ColdBox from this application folder just type uninstall coldbox
.
To update ColdBox from a previous version, just type update coldbox
.
Event handlers are the controller layer in ColdBox and is what you will be executing via the URL
or a FORM
post. All event handlers are singletons, which means they are cached for the duration of the application, so always remember to var scope your variables in your functions.
Tip: For development we highly encourage you to turn handler caching off or you will have to reinit the application in every request, which is annoying. Open the config/ColdBox.cfc
and look for the coldbox.handlerCaching
setting.
Once you started the server in the previous section and opened the browser, the default event got executed which maps to an event handler CFC (controller) handlers/main.cfc
and the method/action in that CFC called index()
. Go open the handlers/main.cfc
and let's explore the code.
Every action in ColdBox receives three arguments:
event
- An object that models and is used to work with the current request
rc
- A struct that contains both URL/FORM variables (unsafe data)
prc
- A secondary struct that is private only settable from within your application (safe data)
This line event.setView( "main/index" )
told ColdBox to render a view back to the user found in views/main/index.cfm
using a default layout, which by convention is called Main.cfm
which can be found in the layouts
folder.
We have now seen how to add handlers via CommandBox using the coldbox create handler
command and also execute them by convention by leveraging the following URL pattern:
Also remember, that if no action
is defined in the incoming URL then the default action of index
will be used.
Remember that the URL mappings support in ColdBox is what allows you to execute events in such a way from the URL. These are controlled by your application router: config/Router.cfc
Now, let's open the handler we created before called handlers/hello.cfc
and add some public and private variables to it so our views can render the variables.
Let's open the view now: views/hello/index.cfm
and change it to this:
If you execute the event now: http://localhost:{port}/hello/index
you will see a message of Hello nobody
.
Now change the incoming URL to this: http://localhost:{port}/hello/index?name=ColdBox
and you will see a message of Hello ColdBox
.
Every time the framework renders a view, it will try to leverage the default layout which is located in layouts/Main.cfm
by convention. This is an HTML file that gives format to your output and contains the location of where the view you want should be rendered.
This location is identified by the following code:
The call to the renderView()
method with no arguments tells the framework to render the view that was set using event.setView()
. This is called a rendering region. You can use as many rendering regions within layouts or even within views themselves.
Named Regions: The setView()
method even allows you to name these regions and then render them in any layout or other views using their name.
Let's create a new simple layout with two rendering regions. Open up CommandBox and issue the following commands:
Open the layouts/Funky.cfm
layout and let's modify it a bit by adding the footer view as a rendering region.
Now, let's open the handler we created before called handlers/hello.cfc
and add some code to use our new layout explicitly via adding a layout
argument to our setView()
call.
Go execute the event now: http://localhost:{port}/hello/index
and you will see the view rendered with the words funky layout
and footer view
at the bottom. Eureka, you have now created a layout.
You can also leverage the function event.setLayout( "Funky" )
to change layouts and even concatenate the calls:
event.setView( "hello/index" )
.setLayout( "Funky" );
Let's complete our saga into MVC by developing the M, which stands for . This layer is all your business logic, queries, external dependencies, etc. of your application, which represents the problem to solve or the domain to solve.
Let's create a simple contact listing, so open up CommandBox and issue the following command:
This will create a models/ContactService.cfc
with a getAll()
method and a companion unit test at tests/specs/unit/ContactServiceTest.cfc
. Let's open the model object:
Notice the singleton
annotation on the component tag. This tells WireBox that this service should be cached for the entire application life-span. If you remove the annotation, then the service will become a transient object, which means that it will be re-created every time it is requested.
Let's mock an array of contacts so we can display them later. We can move this to a SQL call later.
You can then leverage it to mock your contacts or any simple/complex data requirement.
We have now created our model so let's tell our event handler about it. Let's create a new handler using CommandBox:
This will create the handler/contacts.cfc
handler with an index()
action, the views/contacts/index.cfm
view and the accompanying integration test tests/specs/integration/contactsTest.cfc
.
Let's open the handler and add a new ColdFusion property
that will have a reference to our model object.
Please note that inject
annotation on the property
definition. This tells WireBox what model to inject into the handler's variables
scope.
By convention it looks in the models
folder for the value, which in our case is ContactService
. Now let's call it and place some data in the private request collection prc
so our views can use it.
Now that we have put the array of contacts into the prc
struct as aContacts
, let's display it to the screen using ColdBox's HTML Helper.
The ColdBox HTML Helper is a companion class that exists in all layouts and views that allows you to generate semantic HTML5 without the needed verbosity of nesting, or binding to ORM/Business objects.
Open the contacts/index.cfm
and add the following to the view:
That's it! Execute the event: http://localhost:{port}/contacts/index
and view the nice table of contacts being presented to you.
Congratulations, you have made a complete MVC circle!
Out of the box, ColdBox gives you all the RESTFul capabilities you will need to create robust and scalable RESTFul services. Let's add some RESTFul capabilities to our contact listing we created in the previous section.
Tip: You can find much more information about building ColdBox RESTFul services in our
If you know beforehand what type of format you will be responding with, you can leverage ColdBox 5's auto-marshalling in your handlers. By default, ColdBox detects any return value from handlers and if they are complex it will convert them to JSON automatically for you:
That's it! ColdBox detects the array and automatically serializes it to JSON. Easy Peasy!
The request context object has a special function called renderData()
that can take any type of data and marshall it for you to other formats like xml, json, wddx, pdf, text, html
or your own type.
Tip: You can find more information at the API Docs for renderData()
here )
So let's open the handlers/contacts.cfc
and add to our current code:
We have added the following line:
This tells ColdBox to render the contacts data in 4 formats: xml, json, pdf and html. WOW! So how would you trigger each format? Via the URL of course.
ColdBox has the ability to detect formats via URL extensions or an incoming Accepts
header. If no extension is sent, then ColdBox attempts to determine the format by inspecting the Accepts
header. If we still can't figure out what format to choose, the default of html
is selected for you.
Tip: You can also avoid the extension and pass a URL argument called format
with the correct format type: ?format=json
.
Let's add a new route to our system that is more RESTFul than /contacts/index.json
. You will do so by leveraging the application's router found at config/Router.cfc
. Find the configure()
method and let's add a new route:
The route()
method allows you to register new URL patterns in your application and immediately route them to a target event. You can even give it a human readable name that can be later referenced in the buildLink()
method.
We have now created a new URL route called /api/contacts
that if detected will execute the contacts.index
event. Now reinit the application, why, well we changed the application router and we need the changes to take effect.
Tip: Every time you add new routes make sure you reinit the application: http://localhost:{port}/?fwreinit
.
You can now visit the new URL pattern and you have successfully built a RESTFul API for your contacts.
ColdBox provides you with a nice method for generating links between events by leveraging an object called event
that is accessible in all of your layouts/views and event handlers. This event
object is called behind the scenes the request context object, which models the incoming request and even contains all of your incoming FORM
and URL
variables in a structure called rc
.
Tip: You will use the event object to set views, set layouts, set HTTP headers, read HTTP headers, convert data to other types (json,xml,pdf), and much more.
The method in the request context that builds links is called: buildLink()
. Here are some of the arguments you can use:
Edit the views/virtual/hello.cfm
page and wrap the content in a cfoutput
and create a link to the main ColdBox event, which by convention is main.index
.
This code will generate a link to the main.index
event in a search engine safe manner and in SSL detection mode. Go execute the event: http://localhost:{port}/virtual/hello
and click on the generated URL, you will now be navigating to the default event /main/index
. This technique will also apply to FORM submissions:
For extra credit try to use more of the buildLink
arguments.
ColdBox allows you to manipulate the incoming URL so you can create robust URL strategies especially for RESTFul services. This is all done by convention and you can configure it via the application router: config/Router.cfc
for more granular control.
Out of the box we provide you with convention based routing that maps the URL to modules/folders/handlers and actions.
route( "/:handler/:action" ).end()
We have now seen how to execute events via nice URLs. Behind the scenes, ColdBox translates the URL into an executable event string just like if you were using a normal URL string:
/main/index
-> ?event=main.index
/virtual/hello
-> ?event=virtual.hello
/admin/users/list
-> ?event=admin.users.list
/handler/action/name/value
-> ?event=handler.action&name=value
/handler/action/name
-> ?event=handler.action&name=
By convention, any name-value pairs detected after an event variable will be treated as an incoming URL
variables. If there is no pair, then the value will be an empty string.
Tip: By default the ColdBox application templates are using full URL rewrites. If your web server does not support them, then open the config/Router.cfc
and change the full rewrites method to false: setFullRewrites( false ).
CommandBox comes with a coldbox create app
command that can enable you to create application skeletons using one of our official skeletons or :
Advanced : A tag based advanced template
AdvancedScript (default
): A script based advanced template
elixir : A ColdBox Elixir based template
ElixirBower : A ColdBox Elixir + Bower based template
ElixirVueJS : A ColdBox Elixir + Vue.js based template
rest: A RESTFul services template
rest-hmvc: A RESTFul service built with modules
Simple : A traditional simple template
SuperSimple : The bare-bones template
You can find all our template skeletons here:
So let's create our first app using the default template skeleton AdvancedScript:
This will scaffold the application and also install ColdBox for you. The following folders/files are generated for you:
Now let's start a server so we can see our application running:
If you are using CommandBox 3 and below, you will be using a Lucee 4.5 Server.
This command will start a server with URL rewrites enabled, open a web browser for you and execute the index.cfm
which in turn executes the default event by convention in a ColdBox application: main.index
.
Tip: ColdBox Events map to handlers (cfc) and appropriate actions (functions)
That's it, you have just created your first application. Hooray, onward!
Tip: Type coldbox create app help
to get help on all the options for creating ColdBox applications.
ColdBox is a conventions based framework. The location of files and functions matter. Since we scaffolded our first application, let's write down in a table below with the different conventions that exist in ColdBox.
What is the common denominator in all the conventions? That they are all optional.
There will be times when you make configuration or code changes that are not reflected immediately in the application due to caching. You can tell the framework to _reinit _or restart the application for you via the URL by leveraging the special URL variable fwreinit
.
You can also use CommandBox to reinit the application:
Congratulations! Did you finish this guide in less than 60 minutes? If you did please leave us some great feedback below. If you didn't, then please do tell us why, we would love to improve our guides.
You can now move on to the next level in becoming a ColdBox Guru! Choose your path below:
If you run into issues or just have questions, please jump on our and our and ask away.
ColdBox is Professional Open Source under the Apache 2.0 license. We'd love to have your help with the product.
The first step in our journey is to CommandBox. is a ColdFusion (CFML) Command Line Interface (CLI), REPL, Package Manager and Embedded Server. We will be using CommandBox for almost every excercise in this book and it will also allow you to get up and running with ColdFusion and ColdBox in a much speedier manner.
You can download CommandBox from the official site: and install in your preferred Operating System (Windows, Mac, *unix). CommandBox comes in two flavors:
So make sure you choose your desired installation path and follow the instructions here:
CommandBox will resolve coldbox
from ForgeBox (), use the latest version available, download and install it in this folder alongside a box.json
file which represents your application package.
That's it. CommandBox can now track this version of ColdBox for you in this directory. In the we will scaffold a ColdBox application using an application template.
You can find many scaffolding templates for ColdBox in our Github organization:
Please note that we used the ColdFusion function encodeForHTML()
() on the public variable. Why? Because you can never trust the client and what they send, make sure you use the built-in ColdFusion encoding functions in order to avoid XSS hacks or worse on incoming public (rc
) variables.
Tip: Please see the section for in-depth information about them.
This layer is controlled by , the dependency injection framework within ColdBox, which will give you the flexibility of wiring your objects and persisting them for you.
We also have created a project to mock any type of data: . Just use CommandBox to install it: install mockdatacfc
Please check out the API Docs to discover the HTML Helper:
Tip You can find much more information about models and dependency injection in our
You can find much more about routing in our
Tip You can visit our API Docs for further information about the event
object and the buildLink
method: .
This will start up a 5 open source CFML engine (If you are in CommandBox 4). If you would like an Adobe ColdFusion server then just add to the command: cfengine=adobe@{version}
where {version}
can be: 2016,11,10,9.
Tip: You can add a password to the reinit procedures for further security, please see the .
File/Folder Convention | Mandatory | Description |
| false | The application configuration file |
| false | The application URL router |
| false | Event Handlers (controllers) |
| false | Layouts |
| false | Model objects |
| false | CommandBox Tracked Modules |
| false | Custom Modules You Write |
| false | Views |