Skip to main content
November 18, 2008
Question

OnRequest with CFC

  • November 18, 2008
  • 2 replies
  • 952 views
I know that OnRequestStart and OnRequest run at the top of each request, but is there a way to consider a call (invoke) for a CFC as a request ? What I need is to use either OnRequestStart or OnRequest with any invoke for a CFC to do some logic check before it excute the function in the CFC. I may have 2 or 3 invokes in the same page. one more thing just in my case, all the CFCs I have are initialized on Application scope.

Any suggestions ?
This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
November 23, 2008
Qais wrote:
... What I need is to use either OnRequestStart or OnRequest with any invoke for a CFC to do some logic check before it excute the function in the CFC. I may have 2 or 3 invokes in the same page.... one more thing just in my case, all the CFCs I have are initialized on Application scope.

...let's say (for example) I have a CFC call on line 10 to get and display member info, and on line 20 I have another call to different CFC where you want to show some private info for that member and only admin level can see it. and let's assume I have 3 level of permissions (none, basic and advance), for the none level, both shouldn't run and should display something else, for basic they will see only the return for the first call, while advance can see the return of both CFCs, this is just an example and hope I'm explaining it well this time, now some people may say that I could approach this in another way, but I'm just trying to ask people here if one those 2 methods can be called upon each CFC invoke...


I would put code in onRequestStart to determine whether the user's permission level is none, basic or advance. It might likely involve using <cfloginuser>, where the roles attribute can have the value none, basic or advance. I would store that permission information in session scope.

I would then create a CFM page to run the remaining logic. It would have the following logic:

<cfswitch expression="#session.permission#">
<cfcase value="none">
Display something else
</cfcase>
<cfcase value="basic">
create CFC_1 object, CFC_1_obj
call CFC_1_obj.displayMemberInfo();
</cfcase>
<cfcase value="advance">
create CFC_1 object, CFC_1_obj
call CFC_1_obj.displayMemberInfo();

create CFC_2 object, CFC_2_obj
call CFC_2_obj.showPrivateInfoForMember();
</cfcase>
</cfswitch>


Inspiring
November 18, 2008
> I know that OnRequestStart and OnRequest run at the top of each request, but is
> there a way to consider a call (invoke) for a CFC as a request ?

If one calls a CFC method as a web service, it is a fully-fledged request
in itself. I wouldn not recommend this approach though, because there'd be
quite a performance hit.

I think you should ask yourself the question - or explain to us better -
why it is you want/need to re-invokve the processing from onRequestStart()
/ onRequest() *again*, for CFC method calls within the same request. It
sounds to me like the code in your onRequest methods aren't really specific
to the request.

Perhaps you should factor-out the required functionality from your
onRequest method(s), and put it somewhere else, calling it both from the
onRequest and other methods.

You could look at ColdSpring for handling stuff like automatically calling
[other functionality] whenever any old CFC method is called.

--
Adam
November 18, 2008
I need to use one of those methods as a security gateway, so in theory, this should run first (OnRequestStart or OnRequest ), if you pass the security requirments then continue the call/invoke, otherwise reject it before it reach CFC.