Friday, January 25, 2013

Re: Components vs Helpers vs Utilities

On Fri, Jan 25, 2013 at 4:53 PM, tron <tkendal@gmail.com> wrote:
> Im trying to wrap my head around utilzing some functionality at all levels
> of MVC. Right now, I'm writing a component that performs several date
> functions for my app (the date utility is insufficient for what Im doing).
> The problem is I need the these functions in both my models and views as
> well. I'd imagine writing my own Utility is the best option as it can be
> used anywhere but they seem "off limits" as they are only contained within
> the Lib. I also cant find any information on writing a Utility. Yes, I could
> just go write one and add it to Lib/Utilities but I'm looking for some
> feedback on this issue as I've faced it several times. If you need to share
> functionality between all layers of the MVC, is using utilities the only way
> to do it without having to load a component/helper/behavior on the fly?

If CakeTime doesn't have what you want you could extend it.

app/Lib/Utility/MyTime.php :

App::uses('CakeTime', 'Utility');

class MyTime extends CakeTime {

public function foo($bar = null) {

}
}

In your controller/Model/View :

App::uses('MyTime', 'Utility');

...

$foo = MyTime::foo($bar);

But if all you want/need is a plain function (ie. no class) you could
just put in bootstrap.php

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.

No comments: