ZF: Where to put custom function
I have an enCrypt and deCrypt class which I want to use in my whole Zend Framework project without having to declare it on every need, but j开发者_运维知识库ust once. Where should this be done? Thank you for any help...
Zend Framework is quite flexible in implementing things.
You could create an instance in a bootstrap file and save it to the registry; whenever you need to call the class just get it from registry?
You could have every controller extend Zend_Controller_Action and put the two functions into this class (only ideal if you're calling the classes from the controller).
Or, what I did, was make my functions static, register my own library (in your application config.ini file, enter the line: autoloaderNamespaces[] = "MyPrefix_"
, then create a folder in the library folder called MyPrefix) and drop my class in there. When I need it I call $encryptedString = MyPrefix_Crypt::encrypt($string);
and $string = MyPrefix_Crypt::decrypt($encryptedString);
Hope this helps :)
精彩评论