Tuesday, February 22, 2022

[FIXED] How to set variable to other ctp file from an action in CakePHP?

Issue

I am using CakePHP 3. My action is:

function OrderFromReseller($api_key) {
    $this->render('index');
    $this->loadModel('Psetting');
    $products = $this->Psetting->find('all');
    $this->set(compact('products'));
}

Here I render another ctp file called index.ctp. Now 'products' variable is undefined in index. How to set this variable to index.ctp file from this action?


Solution

You need to call $this->render('index') last:-

function OrderFromReseller($api_key) {
    $this->loadModel('Psetting');
    $products = $this->Psetting->find('all');
    $this->set(compact('products'));
    $this->render('index');
}

render() tells Cake to generate the View so anything that follows will not get taken into account by the template as it has already been rendered.



Answered By - drmonkeyninja

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.