http://seancorfield.github.io for newer blog posts." />

An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

Application.cfc - onRequest() and CFCs

April 7, 2005 · 7 Comments

Now that people are starting to use Application.cfc they are starting to trip over a gotcha with onRequest(). Read the LiveDocs reference for the onRequest() method! Or, if you're too lazy to read the documentation (as if!), I'll quote the relevant paragraph here for you:
  • Implement this method only if the following are true:
    • The directory, and any subdirectories affected by this Application.cfc contain CFM files and do not contain any CFC files that are intended to be accessed as web services, using Flash Remoting, or using an event gateway.
    • You want to intercept the request and process it in a special way.
Think about how onRequest() works - it <cfinclude>s the target page in order to complete the request. How could it do that for a CFC request that comes in as a Web Service, Flash Remoting or Event Gateway request? How would it know about arguments etc? Answer: it can't. So, if you use Application.cfc and need onRequest() for manipulating a CFM page request and you want to offer CFCs as Web Services, Flash Remoting or Event Gateways then you need to put them in a separate directory and have another Application.cfc in that directory that does not have an onRequest() method.

Tags: coldfusion

7 responses so far ↓

  • 1 Derek V. // Feb 19, 2009 at 11:02 AM

    Yes, I realize this is almost 4 years old but it was the first google results for &quot;extend application.cfc for web service&quot; but it falls just short of answering my question.

    If you happen to get this, I would appreciate and reply.

    I have a root Application.cfc (x:\projectname\) and have successfully extended that into another folder (x:\projectname\ws\) and use cfNTauthenticate to control access there. I am adding a webservice to this application and would like to extend the root Appliation.cfc so I can use application variables. When extending an Application.cfc file, I understand that if I omit a method, it simply inherits the root. If I include a method, you basically start it from scratch - unless I have that wrong to begin with. But if I include the OnRequstMethod() then the web service (as stated above) is not able to execute. I have this working with Application.cfm but wonder two things.
    a.) Can I define the onRequest method in my (x:\projectname\ws\) Application.cfc but void it out some how, so the web service will not inherit the root CFC and then work?
    b.) Or, can I simply extend the root CFC application scope variables in the (x:\projectname\ws\) Application.cfm?

    Thanks for any help you might have here.
  • 2 Sean Corfield // Feb 19, 2009 at 11:14 AM

    You cannot use onRequest() with web services (or AMF or event gateways). Read the docs - it explains this in detail.

    What you need to do in your sub-Application.cfc is inside onRequestStart(), use structDelete() on THIS and VARIABLES scope to remove onRequest(). Ray Camden blogged this some time ago (he credits the tip to me - I was just slow to blog it).
  • 3 Derek V. // Feb 19, 2009 at 11:15 AM

    AHHHH - after a couple more searches (thought I tried them all) I found what I eventually needed - that seems to work.

    Putting
    &lt;cfscript&gt;
    structDelete(variables,&quot;onRequest&quot;);
    structDelete(this,&quot;onRequest&quot;);
    &lt;/cfscript&gt;

    in my extended Application.cfc.
    (sorry)
  • 4 Sean Corfield // Feb 19, 2009 at 1:08 PM

    @Derek, yup, that's what you need. Glad it works!
  • 5 Stefan // Feb 26, 2009 at 11:45 AM

    A lazy mans solution could also be to add this in onRequestStart of the original, root, Application.cfc:
    &lt;cfif listLast(arguments.targetPage,'.') is 'cfc'&gt;
    &lt;cfset StructDelete( THIS, &quot;OnRequest&quot; )&gt;
    &lt;/cfif&gt;

    It works for me, but maybe it has some evil side effects that I will learn later.
  • 6 Sean Corfield // Feb 26, 2009 at 12:32 PM

    @Stefan, that tends to be my application (in the base CFC) and that's how Fusebox handles it:

    Application.cfc:

          &lt;!--- ensure CFC / Web Service / Flex Remoting calls are not intercepted ---&gt;

          &lt;cfif right(arguments.targetPage,4) is &quot;.cfc&quot;&gt;

             &lt;cfset doCompile = false /&gt;

             &lt;cfset structDelete(variables,&quot;onRequest&quot;) /&gt;

             &lt;cfset structDelete(this,&quot;onRequest&quot;) /&gt;

          &lt;/cfif&gt;
  • 7 Kevin R // Feb 15, 2011 at 6:59 PM

    I'm confused. This seems entirely contradictory to Ben Nadel's post here:

    http://www.bennadel.com/blog/1587-CFM-Templates-And-Remote-CFCs-They-re-All-Just-ColdFusion-Page-Requests.htm

    Indeed, however, when I implement OnRequest() my remote HTTP CFC calls fail. Why does it work for Ben Nadel? What am I missing?

    I wish to implement one thing which OnRequest() seems ideal for:

    (1) Authentication for all requests, which would then create certain variables within the VARIABLES scope.

    When I place my authentication CFC into OnRequest() it works perfectly except then my remote access CFCs fail.

    When I place it into OnRequestStart() I can't tell if it's working or not because the variables don't exist--I've read a little bit about placing/copying variables into the REQUEST scope, so maybe this is the problem, but I don't understand it very well yet.

    Thus,

    (1) Why does OnRequest() work in Ben's article I cite above?

    (2) Should I be using OnRequestStart() instead? What do I need to make the result variables available?

    (3) I need my authentication to run for all HTTP requests: CFM and CFC. Seems I only get CFM's when using OnRequest().

    Much thanks for all your posts. Very helpful. CF seems very arcane to me (as a primarily ASP.NET guy), but I'm learning!

    I'm using CF8, by the way, so the new OnCFCRequest() isn't an option.

Leave a Comment

Leave this field empty