如何将cakePHP组件中函数声明的变量传递给控制器?我似乎无法解决这个问题。
答案 0 :(得分:5)
你可以通过多种方式实现这一目标。基本上,您只需从控制器调用所需的组件功能并返回值:
//In controller
$alteredData = $this->MyComponent->doSomethingWithData($data);
//In component
//You can also pass $data by reference if you want to alter it directly.
public function doSomethingWithData($data){
//alter data in some way
return $newData;
}
Per the documentation,您还可以从组件直接访问控制器,以便从组件中调用控制器方法。
//In component
private $Controller;
public function initialize($controller){
$this->Controller = $controller;
}
public function doSomethingWithData($data){
//alter data in some way
$this->Controller->set('data', $data);
}
答案 1 :(得分:0)
在cakePHP 3.x中(可能仅> 3.5)
// in Component
$this->getController()->set('var', $var);