cakephp-3.0cakephp-3.x

In CakePHP3.6, how to call function of another controller into other controller?


I am using CakePHP3.6 for my project. I have created following function in Options controller:

public function getValue($id = null){ 
$options=$this->Options->find()->where(['id'=>$id])->first();
return $options->value;  
}

then I am calling this function into function of another controller(any xyz controller) as follow:

$r = $this->Options->getValue(1);

Now I am getting error 'Unknown method "getValue"'.

Same Procedure I followed for CakePHP3.2, it worked well. So Please help me, How to call function of another controller into function of other controller for CakePHP3.6?


Solution

  • You should never be calling a function from one controller in another, and I doubt very much that this worked as shown in 3.2. This function looks very much like it should be in the OptionsTable, not OptionsController. The only change you'd need to make is inside the function body, it will just be $this->find(), not $this->Options->find().