简体   繁体   中英

Controller wide array/function in CakePHP

I have a local application that runs on multiple screens which I am in the process of moving over to cakePHP. Each screen represents a different view of a production line and has multiple items on each.

I need to refrenece some sort of controller wide function or array that defines the structure of my application, but I am unsure of the best way to do this. Since it will only be used in one controller, it seems excessive to create a global item. I thought I could include a simple array in the controller, outside of a function, and use it in each function. This didn't work for some reason (probably a good reason).

$structure = array(
    'stage_1'=>array('duration'=>5,'temperature'=>293),
    'stage_2'=>array('duration'=>8, 'temperature=>'280),
    'stage_3'=>array('duration'=>3,'temperature'=>283)
);

So... What is the best way to create a controller wide array or function that can be referenced in the controllers view functions?

Sounds like you forgot the var keyword or didn't use $this->structure to call the variable.

class ExamplesController extends AppController {
    var $name = 'Examples';

    var $structure = array(
        'stage_1'=>array('duration'=>5,'temperature'=>293),
        'stage_2'=>array('duration'=>8, 'temperature=>'280),
        'stage_3'=>array('duration'=>3,'temperature'=>283)
    );

    function action() {
        $this->set( 'structure', $this->structure );
    }
}

You might find reading up on object-oriented programming in PHP helpful when developing with CakePHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM