我在控制器中有一个函数,首先检查a,看看用户是否从网站,facebook或手机提交了表单。
然后根据需要改变布局。但是对于移动设备,我想加载一个不同的视图,因为显示非常不同。
我的问题是来自控制器的数据没有传递到新视图。我得到视图中所有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
当您调用Controller::render
时,我相信视图会立即呈现(并返回)。因此,在调用之前,你必须设置视图期望的变量