Reputation: 3015
i am working on a Cakephp 2.3 ..In my modals i am doing encryption an decryption in these two functions beforeSave and afterFind .. as again and again i have to write this
Security::rijndael($text, Configure::read('constants.crypt_key'), 'encrypt');
so i decided to make a function so i have done this
static public function encrypt($text) {
return Security::rijndael($text, Configure::read('constants.crypt_key'), 'encrypt');
}
static public function decrypt($text) {
return Security::rijndael($text), Configure::read('constants.crypt_key'), 'decrypt');
}
but i want to know where should i write these function.. should it be in app/lib/utility or app/vendors directory and also after suggesting, do tell me how can i access the function in the Model ..how can i import the class in Model..thanks in advance
Upvotes: 0
Views: 211
Reputation: 592
To use a common function on controller side you have to declare it in 'AppController.php' While to use function in view files you can mention it in 'AppHelper.php' And for model you can put it in 'Appmodel.php'
Upvotes: 1
Reputation: 13952
It depends where you want to be calling them from. If you're only calling them from your model (which I think makes sense in your case), then you should place them in AppModel.php
, which all your models inherit from.
however, having seen your previous question, if you're having to write the encrypt/decrypt function "again and again", then you're probably not designing your app very well.
Really, you should only need to call encrypt once, in your beforeSave, and decrypt once, in your afterFind. If you have to call them in one or two other places... OK. But if you're having to call them all over the place, you're going about things the wrong way.
And also, there should be no need to make it a static function.
Upvotes: 0