我有一个有多个函数的控制器,里面有一个变量。我想从另一个函数访问这些变量。有什么想法吗?
class RoomsController extends AppController {
public $helpers = array('Js' => 'Jquery');
public $components = array('RequestHandler');
function test1(){
$balcony = $_REQUEST['qwe'];
$this->set('qwe',$qwe);
}
function test2() {
$occy = $_REQUEST['wer'];
$this->set('wer',$wer);
}
function test3() {
$deck = $_REQUEST['ert'];
$this->set('ert',$ert);
}
function success() {
// i want to use $qwe, $wer and $ert here
}你有什么建议吗?是否必须设置全局变量public $qwe;
谢谢
发布于 2011-10-04 13:20:38
你可以创建控制器属性,但它会破坏框架的设计,所以我建议使用Configuration类,你可以使用这个类来存储变量值和检索,这里有一个文档:
http://book.cakephp.org/view/42/The-Configuration-Class
如果重定向后需要变量的值,可以将变量存储在会话中:
$this -> Session -> write("variable", "value");并检索:
$this -> Session -> read("variable");发布于 2011-10-04 13:26:01
您可以使用CakePHP会话完成此操作
$this->Session->write('qwe', $qwe);\\ in test1
$qwe= $this->Session->read('qwe'); \\ in sucesshttp://book.cakephp.org/view/1312/write
虽然代码中有许多错误和歧义。
function test1(){
$balcony = $_REQUEST['qwe'];
$this->set('qwe',$qwe);
}在这里,当你用$qwe设置qwe时,你在$balcony中获取请求值。你在所有函数中都在做同样的事情,我认为你必须检查这一点。
同样,正如deceze所说,您没有正确使用成功流函数。
https://stackoverflow.com/questions/7643571
复制相似问题