John regularly blogs about ColdFusion, JavaScript and other web technologies and contributes to several FOSS projects. His hobbies include writing in the third person. John is a DZone MVB and is not an employee of DZone and has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

Really simple CMS with FW/1 and ColdFusion ORM

06.01.2011
| 7364 views |
  • submit to reddit

I recently wanted to create a simple, lightweight Content Managed site and decided to use FW/1 and of course ColdFusion's excellent ORM capabilities.

FW/1 is incredibly easy to get started with, you just need to download a single cfc and get Application.cfc to extend it.

As my site will have dynamic pages which can be added or deleted at any time, then I don't want to have to create specific views and controllers (as I don't know what the pages are yet!), luckily F/W1 has a onMissingView method which is called when a view is not found.

In the onMissingView I'm going use EntityLoad to get the page content from the database. To do that I'll need the define my Content object.

/**
* I am the Content object which maps to the contents database table
*/
component persistent="true" table="contents"
{
  property name="id" column="content_id" generator="native";
  property name="guid" column="content_guid"; // used for pretty urls
  property name="title" column="content_title";
  property name="content" column="content_content";
}

I'll need a view page to show the page content stored in the database.

<!--- 
views/content.cfm
I'm a simple view to display CMS content 
--->
<cfoutput>
<h1>#rc.Content.getTitle()#</h1>

#rc.Content.getContent()#
</cfoutput>

It's a good idea to trap any requests to pages that no longer exist, so we need a 404 page.

<!--- 
views/notfound.cfm
I'm a simple 404 view
--->
<cfoutput>
<h1>Page Not found</h1>
<p>Sorry, the page you requested (#getFullyQualifiedAction()#) has not been found.</p>
<cfoutput>

A layout would be kinda useful!

<!--- 
layouts/default.cfm
I am the default layout
--->
<cfoutput>
<html>
<head>
<title>#rc.Content.getTitle()#</title>
</head>
<body>
<ul id="nav">
<cfloop array="#rc.menu#" index="local.thispage">
  <li><a href="#buildURL( local.thispage.getGuid() )#">#local.thispage.getTitle()#</a>
</cfloop>
</ul>

<div id="content">#body#</div>

</body>
</html>
</cfoutput>

Now we need to set up the Application.cfc

component extends="org.corfield.framework"
{

  APPLICATION_ROOT = GetDirectoryFromPath( GetCurrentTemplatePath() );
   
  this.name = ReReplace( UCase( APPLICATION_ROOT ), "[^A-Z]", "", "all" );
  this.mappings["/model"] = APPLICATION_ROOT & "model";
  
  // ORM Setttings
  this.datasource = "simplecms";
  this.ormenabled = true;
  this.ormSettings.flushAtRequestEnd = false;
  this.ormSettings.cfclocation = this.mappings["/model"];
  
  // FW/1 Setttings
  variables.framework = {
    generateSES = true
  };

  // executes when a view is not found
  any function onMissingView( required rc )
  {
    var filter = {
      guid = getFullyQualifiedAction()
    };
    
    rc.Content = EntityLoad( "Content", filter, true );
    if ( !IsNull( rc.Content ) )
    {
      // use the content view to display content
      return view( 'content' );
    }
    else
    {
      // create a 404 response header
      var pc = getpagecontext().getresponse();
      pc.getresponse().setstatus( 404 );
      // set the page title
      rc.Content = EntityNew( "Content" );
      rc.Content.setTitle( "Page Not Found" ); 
      // display the 404 page
      return view( 'notfound' );
    }
  }
  
  // executes per request
  void function setupRequest()
  {
    
    // get all pages for the navigation
    rc.menu = EntityLoad( "Content" );
  }
  
}

Finally, we need to create a blank index.cfm file in the application root. Now when you hit a URL such as; http://localhost/simplecms/index.cfm/my-new-page. You'll get the 404 page. If you add a page to your database with the content_guid field set to "my-new-page.default", it will magically appear.

Obviously my application is more complex than this, but I thought it would be a good example of how simple it is to build a fairly powerful system quickly in ColdFusion and I haven't mentioned a beanFactory once!

References
Published at DZone with permission of John Whish, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Kathy John replied on Tue, 2012/02/21 - 1:11pm

great example of how frameworks can handle complexity for you. Amazing how much ground you can cover in just a few dozen lines.

What I find interesting is the fact that FW/1 accomplishes what it does while remaining just a single file. A sleek nutcracker to Hibernate's sledgehammer. But as your code shows, CF can do most of the heavy lifting and wielding of that sledgehammer for you.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.