Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing component variables to controller in cakePHP

how do I pass a variable declared in a function in a cakePHP component to a controller? I can't seem to work this out.

like image 562
user765368 Avatar asked May 13 '26 01:05

user765368


1 Answers

You can do it a number of ways. Basically, you just call the component's functions that you need from the controller and return values:

//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, you also have direct access to the controller from the component so you can call controller methods from the component.

//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);
}
like image 183
Scott Harwell Avatar answered May 15 '26 02:05

Scott Harwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!