Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top