cakephp change view不会从控制器传递数据

时间:2011-11-24 17:44:10

标签: php cakephp

我在控制器中有一个功能,首先检查用户是否从网站,脸书或手机上提交表格。

然后根据需要更改布局。但是对于移动设备,我想加载一个不同的视图,因为显示器是完全不同的。

我的问题是来自控制器的数据没有传递给新视图。我得到视图中所有php的未定义变量和参数。 php代码在所有视图中完全相同,它只是我在移动视图中更改div的布局。

我不明白为什么会发生这种情况?

控制器

function availability() {

    if ($_REQUEST['from'] == 'facebook') {
        $this->layout = 'facebook';
    }elseif ($_REQUEST['from'] == 'website'){
        $this->layout = 'res';
    }elseif ($_REQUEST['from'] == 'mobile'){
        $this->layout = 'mobile_layout';
        $this->render( 'mobile' );
    };

    $this->newSession();

    $msg[0] = array(); // 0 = bad messages
    $msg[1] = array(); // 1 = good messages 

    if(isset($_REQUEST['date_start'],$_REQUEST['date_end'])){
            $data['Availability']['date_start'] = $_REQUEST['date_start'];
            $data['Availability']['date_end'] = $_REQUEST['date_end'];
    }

    if(!isset($_REQUEST['voucher_code'])){
        $_REQUEST['voucher_code'] = 0;
    }   

移动视图中的一个错误,res_controller中的第83行是$ this-> render('mobile');:

 Notice (8): Undefined variable: availability [APP/views/res/mobile.ctp, line 16]
Code | Context

include - APP/views/res/mobile.ctp, line 16
View::_render() - CORE/cake/libs/view/view.php, line 731
View::render() - CORE/cake/libs/view/view.php, line 426
Controller::render() - CORE/cake/libs/controller/controller.php, line 909
ResController::availability() - APP/controllers/res_controller.php, line 83
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83

1 个答案:

答案 0 :(得分:1)

当您致电Controller::render时,我相信该视图会立即呈现(并返回) 。因此,在调用之前,您必须设置视图所期望的任何变量。