简体   繁体   中英

Cakephp calling a function between Model and View

Let say i have a function:

function getCarName() {
   return array(1 => 'BMW', 2 => 'MERCEDEZ BENZ', 3 => 'RENAULT');
}

I will use this function on both model and view, should i create two same function on AppHelper and Car Model? What is correct way to achieve this?

Having two functions doing the same thing is usually a bad idea as it violates the don't repeat yourself principle.

Instead, I would add the function to your Car model and then pass the data from your controller(s) to your view(s) with something like:

$this->set('carNames', $this->Car->getCarNames());

The car names are then available in the view via the $carNames variable.

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