0

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

2 Answers 2

1

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'

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

@joshuapaling thanks for your reply... you are saying that "i am not designing well" ..so tell me what else i do .. because for example if i want to encrypt email and username which is in my User model obviously i write afterfind and beforesave function in the Users Modal .. and if i want to encrypt the fields of Posts table .i will write functions in Post modal ..so on ... so is dat something i am doing wrong ???
Oh, OK, in that case it sounds like you're calling encrypt/decrypt from a few different models. That's OK, and in that case, use AppModel.php, as I said. I thought you might be calling them from various layers (ie, sometimes from the model, sometimes from the controller, sometimes from the view), and that would be a sign of poor design. But what you're doing sounds OK, presuming you do have a legitimate reason to encrypt this data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.