1

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?

Gurpreet Kaur
  • 19
  • 1
  • 7

2 Answers2

3

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().

Greg Schmidt
  • 5,010
  • 2
  • 14
  • 35
  • 1
    No we can't use `$this->find()` instead of `$this->Options->find()`. If you will use directly `$this->find()`, you will get the error "Call to undefined method App\Controller\OptionsController::find()". you can try it. And Please tell me the **reason** why we should never call a function from another controller ? And Yes, It worked very well in 3.2. No need to doubt because I have already used it. – Gurpreet Kaur Sep 08 '18 at 17:51
  • 1
    You should never call a function from another controller, because that breaks the separation of concerns. Controllers can very easily get access to any table in the system, CakePHP is set up to make this happen. But to get another controller, you would need to explicitly create an object of that type (e.g. "$options_controller = new \App\Controller\OptionsController()`, and make efforts to ensure that it's correctly initialized. That's why I'm saying I don't expect it worked *as shown* in 3.2, because `$this->Options` looks like a reference to the `OptionsTable`, not `OptionsController`. – Greg Schmidt Sep 09 '18 at 01:50
  • And my suggestion of using `$this->find()` is assuming that the function is moved into the `OptionsTable`. You clearly didn't try that, based on the error message reported; you just tried removing `->Options` from the function where it already is. Which, as I hope I've explained, is the wrong spot. Not sure why you're resistant to moving the function to a place where it would be easily accessible to do exactly what you say you want to do. – Greg Schmidt Sep 09 '18 at 01:53
  • thank you. I am able to call function successfully by creating object of OptionsController class as per your suggestion i.e `$options_controller = new \App\Controller\OptionsController()` . I understand the concerns of separation of cakephp, But as per my project I need it. Because, I need to fetch some data by passing a value to it and I want to avoid to write same code again and again. – Gurpreet Kaur Sep 10 '18 at 03:49
  • 1
    @GurpreetKaur That's _not_ a suggestion, it's an example of what you should **_not_** do! Manually instantiating controllers is the wrong thing to do 99.999% of the time, just like calling methods of other controllers in general. The suggestion here is to put the logic into your model (your table class), that's the right place for it! – ndm Sep 10 '18 at 10:05
  • Your desire of fetching data by passing a value to a function is completely met by putting this function into the `OptionsTable`, as I have suggested twice already. – Greg Schmidt Sep 10 '18 at 15:27
  • Looks like I can't edit my previous comment to fix the code formatting. :-( – Greg Schmidt Sep 10 '18 at 15:28
0
class FirstController extends AppController
{
public function getValue($id = null){ 
  $options=$this->Options->find()->where(['id'=>$id])->first();
  return $options->value;  
}
}

class SecondController extends AppController
{
public function anotherAction($id = null){
 $anotherController =  new \App\Controller\FirstController();  
 $r = $anotherController->getValue($id) ;   
}
}
  • 1
    Code dumps do not make for good answers. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](//stackoverflow.com/help/how-to-answer). This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – John Conde Feb 26 '21 at 00:33