How to call a controller function in an another controller function

525 views
Skip to first unread message

Jermy

unread,
Oct 14, 2010, 11:29:19 AM10/14/10
to framework-one
Hi List,

seems a dumb question. I am not able to make a call of controller
function in the same controller.

I have a function (controller.authenticate)which check the login type.
(login type comes from the login page)

controller
*************
<cffunction name="authenticate">
<cfargument name="rc" required="true" type="struct">
<cfset rc.login_type =
variables.fw.service('loginauth.checkLoginType','login_type',rc)>
</cffunction>

->services.checkLoginType returns String.



<cffunction name="endAuthenticate">
<cfif rc.login_type EQ "">
<cfset variables.fw.redirect('login.login')>
<cfelse>
<cfif rc.login_type EQ "emailID">
<cfset authenticateOnEmailID(rc)>
<cfelse>
<cfset authenticateOnUserAccount(rc)>
</cfif>
</cfif>
</cffunction>


Thanks,
Jermy

Dave Anderson

unread,
Oct 14, 2010, 3:59:27 PM10/14/10
to framework-one
First off, you should have startAuthenticate() and endAuthenticate()
OR just authenticate() -- not authenticate() and endAuthenticate().
It's a coding best-practices thing.

I'd resolve what you're trying to do by refactoring the code a bit.
You've got a 'loginauth' service, so use it. Put
authenticateOnEmailID() and authenticateOnUserAccount() in the
loginauth service, then update your checkLoginType() method to do what
you're trying to do in endAuthenticate(). Then endAuthenticate() in
the controller can simply check for a blank value for login_type and
redirect on that condition, and do nothing else.

Sound about right?

Sean Corfield

unread,
Oct 14, 2010, 7:37:53 PM10/14/10
to framew...@googlegroups.com
On Thu, Oct 14, 2010 at 8:29 AM, Jermy <jerm...@gmail.com> wrote:
>                <cfset rc.login_type =
> variables.fw.service('loginauth.checkLoginType','login_type',rc)>

The service() API call _queues up the call_ - it does _not_ execute
it. So it won't return anything useful.

Also, do _not_ pass rc to the service call. FW/1 already passes
elements of rc to the service call. So you should have just:

<cfset variables.fw.service( 'loginauth.checkLoginType', 'login_type' ) />

> <cffunction name="endAuthenticate">
>        <cfif rc.login_type EQ "">

At this point, rc.login_type should be set to the return value of your
service method so you should be OK.

>                <cfset variables.fw.redirect('login.login')>
>                <cfelse>
>                        <cfif rc.login_type EQ "emailID">
>                                <cfset authenticateOnEmailID(rc)>
>                        <cfelse>
>                                <cfset authenticateOnUserAccount(rc)>
>                        </cfif>
>                </cfif>
> </cffunction>

Stylistically, calling other items in a controller is poor style:
controller methods should not call other controller methods.

Controllers should delegate to services.

Your service method should determine the login type and then do the
authentication and return a result that tells the controller whether
login was successful or not.
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

Jermy

unread,
Oct 15, 2010, 8:53:39 AM10/15/10
to framework-one
Thanks Dave for your suggession.

Controllers should delegate to services-> make sense.
Thanks Sean.

-Jermy

On Oct 15, 4:37 am, Sean Corfield <seancorfi...@gmail.com> wrote:
> Railo Technologies, Inc. --http://getrailo.com/
> An Architect's View --http://corfield.org/
Reply all
Reply to author
Forward
0 new messages