PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

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

 February 22, 2022     cakephp, cakephp-3.0     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing