Extending Application.cfc - Exercise

Contact Us or call 1-877-932-8228
Extending Application.cfc - Exercise

Extending Application.cfc

Duration: 20 to 30 minutes.

In this exercise, you will create an Application.cfc file that outputs a header on every page. In a subdirectory, you will create another Application.cfc file that changes the header.

  1. Open CFCs/Exercises/Animals/Application.cfc and review the onRequestStart method. You will see that it now outputs a simple navigation bar.
  2. Write code so that pages in the Dogs, Cats, and Mice directories get their own navigation bars, which each have a single link back to Animals/index.cfm.

Change the footer on pages in the Mice directory to include the text "Mice are cool!".

Solution:

CFCs/Solutions/Animals/Dogs/Application.cfc
<cfcomponent extends="Animals.Application" output="yes">
	<cffunction name="onRequestStart" output="yes">
		<cfif NOT isDefined("URL.who") OR URL.who NEQ "dog">
			You must be a dog to visit this page.
			<cfabort>
		<cfelse>
			<cfoutput>
			<div>
				<a href="../index.cfm?who=#URL.who#">Animal Home</a>
			</div>
			</cfoutput>
		</cfif>
	</cffunction>
</cfcomponent>

Challenge Solution:

CFCs/Solutions/Animals/Mice/Application.cfc
<cfcomponent extends="Animals.Application" output="yes">
	<cffunction name="onRequestStart" output="yes">
		<cfif NOT isDefined("URL.who") OR URL.who NEQ "mouse">
			You must be a mouse to visit this page.
			<cfabort>
		<cfelse>
			<cfoutput>
			<div>
				<a href="../index.cfm?who=#URL.who#">Animal Home</a>
			</div>
			</cfoutput>
		</cfif>
	</cffunction>
	<cffunction name="onRequestEnd">
		<cfargument type="String" name = "targetTemplate" required="true"/>
		<hr/>
		&copy; Animals Incorporated. Mice are cool!
	</cffunction>
</cfcomponent>
Next