如何将控制器变量传递给CakePHP中的视图?

Cho*_*mid 0 php mysql cakephp

这是我的控制器:

class MyWorksController extends AppController{
    function index(){
        $workLevel = $this->getWorkLevel();
        if($workLevel == 3){
            $recreationID = $this->getRecessId($workLevel );
            $this->set('recreationID', $recreationID);
            $this->redirect('/MyWorks/report1/');
        }
        else{ 
            $this->redirect('/MyWorks/report2/');
        }     
    }
    function report1(){}

    function report2(){}
}
Run Code Online (Sandbox Code Playgroud)

我想通过以下方式将$ recreationID值发送到我的视图页面的表单:

echo $form->create('FishingTimeAllocations', array('tmt:validate'=>'true', 'action'=>'index', 'recreationID '=>$recreationID ));?>
Run Code Online (Sandbox Code Playgroud)

但是,我没有成功这样做,我一直得到的错误是这样的:

Undefined variable: recreationID 
Run Code Online (Sandbox Code Playgroud)

我的代码中有什么问题,以及完成所需操作的正确方法是什么?

CakePHP版本1.2,PHP版本5.2,MySQL版本5.1.36.(我知道我正在运行这些软件的旧版本 - 目前我无能为力).

Ana*_*Die 10

您无法将变量set()用于其他操作.如果要将变量用于其他页面(视​​图文件),则需要使用以下两种方法:

  1. 在会话中设置然后使用它.
  2. 传递为url参数.

注意: -在这两种方式中会话方式更安全.