3

I am using Cakephp 2.3 , i want to perform lots of common calculation in my several controllers. so i write those functions in my appcontroller and i cached some datas.. but what happens is my appController will become fatty.. so i create some component to perform these actions.. i dont know my approach is right or not..? please suggest..

  1. i want to use cache in my component, i tried this code. but nothing is cached..

        public $helpers =array('Cache');
    

and

     $result = Cache::read('fm_data', 'long');
       if (!$result) {
       $result =
 $this->TFmStation->find('all',array('contain'=>array('TLocation',
            'TLanguage','TMediaOrg','TContactPerson',
                    'TAddress','TFmProgram'=>array('TTargetGroup'))));
                       Cache::write('fm_data', $result, 'long');
                }
 return $result;
  1. please help me to how to use cache in component
  2. how to effectively use component class in cakephp in the case of more common functions in the application.. when i write these functions in appController it load all the functions so according to memory prespective, how to effectively use component
SibinF
  • 385
  • 1
  • 7
  • 25
  • 1
    If you want to save your data only for one page-lifecycle (one request and response) I suggest do implement a Model (e.g. `Calculations`) that contains your business logic for these calculations and you have access in each controller. – Simon Apr 25 '14 at 09:02
  • 1
    If you want to save calculated data separated from clients over multiple requests, it is possible to save the calculated data in sessions or cookies. – Simon Apr 25 '14 at 09:08
  • 1
    And at last, calculated data can be saved on client-side via JavaScript in Browser. If you need these calculation-datasets for calculations on server-side, you can send them via ajax. NOTE: Do not store sensible data (e.g. passwords) on client-side. – Simon Apr 25 '14 at 09:12
  • @SimontheSourcecoderer very good suggestion. Help me a lot. – SibinF Apr 25 '14 at 09:44
  • @SimontheSourcecoderer the problem is , i have 7 table data that are not changed frequently, so i thought best way is load these data on start. i kept it in appcontroller and cached these data. but i need to add around 20 functions for manipulating these array and get the result. writing 20 functions in appcontroller is not so good it become fatty. thats why i categorize these functions and create 3 components, so where ever it needed i can load these component and call these functions. but i cant access cached data in component ? any way? what about creating custom classes and functions? – SibinF Apr 25 '14 at 09:50

1 Answers1

1

Model TFmStation is best place to have the above logic. Components are there for generic functionality like UploadComponent, EmailComponent, RecaptchaComponent etc. If your logic part have something to do with model, then it should go into that model.

A Model can be called from AppController.php in a similar fashion as you calling your Component.

Ish
  • 28,486
  • 11
  • 60
  • 77