Calling a function from another controller :)

853 views
Skip to first unread message

Tim B

unread,
Jul 26, 2012, 5:03:33 PM7/26/12
to cfwh...@googlegroups.com
Well I've searched and even found the question but no answer.

Say you absolutely had no choice but to call a function from another controller. How would you do it?

Also, I thought I heard that public functions were accessible from anywhere in the application.

As always help is very much appreciated.

Thanks, Tim

Andy Bellenie

unread,
Jul 26, 2012, 5:05:25 PM7/26/12
to cfwh...@googlegroups.com
You never have no choice... if the function is required from a different controller, it's probably in the wrong place.

If you really need to call the same function from multiple controllers... put in in the base controller.cfc




--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/mkXjFv6S7GsJ.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

Tim Badolato

unread,
Jul 26, 2012, 5:14:28 PM7/26/12
to cfwh...@googlegroups.com
The reason I'd like call from other controllers is readability. Let me explain:

Say I have a getCart function inside of a Cart controller. This function needs to be accessible from everywhere so I move it to the functions.cfm file. Months later I come back and say "Hey, where'd I put that pesky getCart function". Well logically I would go "Oh, of couse it's probably in the cart controller with the rest of the cart functions". But when I don't find it I do I search and finally find it in the functions.cfm file with a mish mash of a bunch of other random functions.

The above is my current thinking on this. My understanding of wheels controllers is probably the culprit here but I am learning so please set me straight :)

Thanks!


Andy Bellenie

unread,
Jul 26, 2012, 5:17:35 PM7/26/12
to cfwh...@googlegroups.com
Why can't the getCart() method go in the base controller? And what does it do?

Tim Badolato

unread,
Jul 26, 2012, 5:22:12 PM7/26/12
to cfwh...@googlegroups.com
The getCart function puts together the client's shopping cart by making calls to various functions within the cart controller. I guess I really just want to be able to group things together instead of putting things that need to be shared in some obscure place (ie, base controller, functions.cfm, etc)

Mohamad El-Husseini

unread,
Jul 26, 2012, 9:12:17 PM7/26/12
to cfwh...@googlegroups.com
Shouldn't it belong to the model then, rather than the controller? It sounds too me like you might have a bit too much logic in the controller... speculating here!


On Thursday, July 26, 2012 5:22:12 PM UTC-4, Tim B wrote:
The getCart function puts together the client's shopping cart by making calls to various functions within the cart controller. I guess I really just want to be able to group things together instead of putting things that need to be shared in some obscure place (ie, base controller, functions.cfm, etc)


On Thu, Jul 26, 2012 at 4:17 PM, Andy Bellenie <andybe...@gmail.com> wrote:
Why can't the getCart() method go in the base controller? And what does it do?
On 26 July 2012 22:14, Tim Badolato <t...@dreamstonemedia.com> wrote:
The reason I'd like call from other controllers is readability. Let me explain:

Say I have a getCart function inside of a Cart controller. This function needs to be accessible from everywhere so I move it to the functions.cfm file. Months later I come back and say "Hey, where'd I put that pesky getCart function". Well logically I would go "Oh, of couse it's probably in the cart controller with the rest of the cart functions". But when I don't find it I do I search and finally find it in the functions.cfm file with a mish mash of a bunch of other random functions.

The above is my current thinking on this. My understanding of wheels controllers is probably the culprit here but I am learning so please set me straight :)

Thanks!


On Thu, Jul 26, 2012 at 4:05 PM, Andy Bellenie <andybe...@gmail.com> wrote:
You never have no choice... if the function is required from a different controller, it's probably in the wrong place.

If you really need to call the same function from multiple controllers... put in in the base controller.cfc

On 26 July 2012 22:03, Tim B <t...@dreamstonemedia.com> wrote:
Well I've searched and even found the question but no answer.

Say you absolutely had no choice but to call a function from another controller. How would you do it?

Also, I thought I heard that public functions were accessible from anywhere in the application.

As always help is very much appreciated.

Thanks, Tim

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/mkXjFv6S7GsJ.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

James Holmes

unread,
Jul 27, 2012, 10:15:30 AM7/27/12
to cfwh...@googlegroups.com

Agreed. Inject a service into both controllers and let the service do the work.

To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/csVaaJApQ1QJ.

To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.

Doug Boude

unread,
Jul 27, 2012, 10:19:08 AM7/27/12
to cfwh...@googlegroups.com
One excellent way to manage and perform service injection into controllers is via an IOC framework such as Wirebox. There are several IOC plugins available for wheels as well.

Yannick Morin

unread,
Jul 27, 2012, 10:20:25 AM7/27/12
to cfwh...@googlegroups.com
I'm with @Mohamad I love fat Model and skinny Controller

Chris Peters

unread,
Jul 27, 2012, 10:24:26 AM7/27/12
to cfwh...@googlegroups.com
You can use as many levels of inheritance as you'd like to group related functionality. Not everything needs to be dumped into Controller.cfc.

// Controller.cfc
component extends="Wheels" { ... }

// ShoppingBase.cfc
component extends="Controller" {
    private function getCart() { ... }
}

// Orders.cfc
component extends="ShoppingBase" { ... }

// Checkouts.cfc
component extends="ShoppingBase" { ... }

// NothingToDoWithShopping.cfc
component extends="Controller" { ... }

Now you have this hierarchy where everything inherits from its parent(s).

Controller
    ShoppingBase
        Orders
        Carts
    NothingToDoWithShopping

I agree with the sentiments that you should be putting as much logic as possible into the model or a library CFC.

Chris Peters

unread,
Jul 27, 2012, 10:26:47 AM7/27/12
to cfwh...@googlegroups.com
I meant for it to be like this. Sorry.

// Controller.cfc
component extends="Wheels" { ... }

// ShoppingBase.cfc
component extends="Controller" {
    private function getCart() { ... }
}

// Cart.cfc
component extends="ShoppingBase" { ... }

// Orders.cfc
component extends="ShoppingBase" { ... }

// NothingToDoWithShopping.cfc
component extends="Controller" { ... }

Now you have this hierarchy where everything inherits from its parent(s).

Controller
    ShoppingBase
        Orders
        Cart
    NothingToDoWithShopping

Tim Badolato

unread,
Jul 27, 2012, 10:40:05 AM7/27/12
to cfwh...@googlegroups.com
Wow, thanks for all of the answers. Very good ones at that.

@Chris - Your answer was exactly what I was looking for. Thanks.

@
Mohamad & Chris  - K, I agree with keeping logic in models. But since models represent database tables shouldn't they contain only logic that is related to a specific table. Let me give you a scenario to help explain my question: Alright so I have several cart functions that make various calls to different models to manage the cart and then stores the cart struct in the client's session. Now since these cart functions aren't specific to any one model, where should I put them?

Chris Peters

unread,
Jul 27, 2012, 10:44:02 AM7/27/12
to cfwh...@googlegroups.com
I'll give you the classic "it depends" answer. Can you list out some examples?

Tim Badolato

unread,
Jul 27, 2012, 10:51:42 AM7/27/12
to cfwh...@googlegroups.com
Examples? Heres the cart code if thats what you mean:
https://gist.github.com/3188466

tpet...@gmail.com

unread,
Jul 27, 2012, 11:41:35 AM7/27/12
to cfwh...@googlegroups.com
tim,

personally all the methods that you have in your example should not be within the controller. I agree that this is something that should be in a service or its own class. doug wrote up a great blog post about using services:


here is my post on the subject also:


I really think that putting those method inside their own class will make life A LOT easier for you in the long run.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+unsubscribe@googlegroups.com.

Tim Badolato

unread,
Jul 27, 2012, 11:56:39 AM7/27/12
to cfwh...@googlegroups.com
Sweet, thanks, I'll take a look.

To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/6qxsnXYFnK4J.

To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.

Tim B

unread,
Jul 27, 2012, 5:00:31 PM7/27/12
to cfwh...@googlegroups.com
@Tony - Okay, I've got the service up and running in accordance with Doug's code and your modification. However I am unable to access/modify the params scope from the service which is a pretty big deal for me. Any way to do this? Thanks!

Chris Peters

unread,
Jul 27, 2012, 5:03:43 PM7/27/12
to cfwh...@googlegroups.com
Do you call the service from the controller? Pass the params into whatever method you're calling. The controller should be the only thing to know about the params struct and should have to pass it to other layers.

To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/pBrx2YSTz5gJ.

To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.

tpet...@gmail.com

unread,
Jul 28, 2012, 11:39:06 AM7/28/12
to cfwh...@googlegroups.com
@tim,

@chris is 100% correct. Services (by design) are isolated from the rest of your application and no nothing about it. If you need to alter the params scope, you will need to pass it into your service.
Reply all
Reply to author
Forward
0 new messages