在 CakePHP 控制器中呈现元素会抛出缺少视图错误
Rendering of Element in CakePHP Controller throws Missing View error
我正在尝试在控制器内渲染视图并使用 ajax 加载它。
它在我的本地系统中工作正常,但在服务器中它抛出错误:
Error: The view for MyController::myView() was not found.
这是我的代码
控制器:
public function creativeForms(){
$this->layout = 'ajax';
$createdForm = '';
if(!empty($this->request->data['id'])){
$request = array();
$requestFields = array();
if(isset($this->request->data['req_id']) && !empty($this->request->data['req_id'])) {
App::import('Model', 'RequestField');
$RequestField = new RequestField();
$request = $RequestField->find('list',array('fields'=>array('RequestField.form_field_id','RequestField.value'),'conditions'=>array('RequestField.request_id'=>$this->request->data['req_id'])));
$requestFields = $RequestField->find('list',array('fields'=>array('RequestField.form_field_id','RequestField.id'),'conditions'=>array('RequestField.request_id'=>$this->request->data['req_id'])));
}
$form = $this->FormField->Form->findByid($this->request->data['id']);
$createdForm = $this->createForm($form,$request,$requestFields);
}
echo $createdForm;
exit;
}
public function createForm($form , $data= NULL,$dataMap=array()){
$createForm ='';
foreach($form['FormField'] as $FormField){
$this->set('FormField',$FormField);
$this->set('data',$data);
$this->set('dataMap',$dataMap);
switch ($FormField['field_type']){
case 'textbox':
$createForm .= $this->render('../elements/fields/textbox');
break;
case 'textarea':
$createForm .= $this->render('../elements/fields/textarea');
break;
case 'radio':
$createForm .= $this->render('../elements/fields/radio');
break;
case 'checkbox':
$createForm .= $this->render('../elements/fields/checkbox');
break;
case 'file':
$createForm .= $this->render('../elements/fields/upload');
break;
case 'datefield':
$createForm .= $this->render('../elements/fields/datefield');
break;
case 'select':
$createForm .= $this->render('../elements/fields/select');
break;
}
}
return $createForm;
}
datefield.ctp
$field = $this->form->hidden('RequestField.'.$FormField['id'].'.type',
array('value'=>'datefield'));
$required = '';
if($FormField['field_required']){
$required = ' required';
}
if(isset($data[$FormField['id']])){
$field .= $this->form->hidden('RequestField.'.$FormField['id'].'.id',
array('value'=>$dataMap[$FormField['id']]));
$field .= $this->Form->input('RequestField.'.$FormField['id'].'.value',
array('value'=>$data[$FormField['id']],'type'=>'text',
'required'=>$FormField['field_required'],
'label'=>$FormField['field_label'],'class'=>'form-control default date-picker',
'div'=>array('class'=>'form-group'.$required)));
} else {
$field .= $this->Form->input('RequestField.'.$FormField['id'].'.value',
array('type'=>'text','required'=>$FormField['field_required'],
'label'=>$FormField['field_label'],'class'=>'form-control default-date-picker',
'div'=>array('class'=>'form-group'.$required)));
}
echo $field;
我试过用以下方法解决它:
$view = new View($this, false);
$view->set(compact('foo', 'bar')); // set variables
$view->viewPath = 'elements'; // render an element
$html = $view->render('message');
在您的代码中,您正在引用文件夹:
app/View/elements
什么时候应该
app/View/Elements
第一行适用于 Windows(因为它使用 case-insensitive 文件和目录命名约定),但不适用于 Unix-like 操作系统。
尝试将您的代码更改为:
$this->viewPath = 'Elements';
$this->render('fields/upload');
$this->render('fields/checkbox');
//and so on
我正在尝试在控制器内渲染视图并使用 ajax 加载它。
它在我的本地系统中工作正常,但在服务器中它抛出错误:
Error: The view for MyController::myView() was not found.
这是我的代码
控制器:
public function creativeForms(){
$this->layout = 'ajax';
$createdForm = '';
if(!empty($this->request->data['id'])){
$request = array();
$requestFields = array();
if(isset($this->request->data['req_id']) && !empty($this->request->data['req_id'])) {
App::import('Model', 'RequestField');
$RequestField = new RequestField();
$request = $RequestField->find('list',array('fields'=>array('RequestField.form_field_id','RequestField.value'),'conditions'=>array('RequestField.request_id'=>$this->request->data['req_id'])));
$requestFields = $RequestField->find('list',array('fields'=>array('RequestField.form_field_id','RequestField.id'),'conditions'=>array('RequestField.request_id'=>$this->request->data['req_id'])));
}
$form = $this->FormField->Form->findByid($this->request->data['id']);
$createdForm = $this->createForm($form,$request,$requestFields);
}
echo $createdForm;
exit;
}
public function createForm($form , $data= NULL,$dataMap=array()){
$createForm ='';
foreach($form['FormField'] as $FormField){
$this->set('FormField',$FormField);
$this->set('data',$data);
$this->set('dataMap',$dataMap);
switch ($FormField['field_type']){
case 'textbox':
$createForm .= $this->render('../elements/fields/textbox');
break;
case 'textarea':
$createForm .= $this->render('../elements/fields/textarea');
break;
case 'radio':
$createForm .= $this->render('../elements/fields/radio');
break;
case 'checkbox':
$createForm .= $this->render('../elements/fields/checkbox');
break;
case 'file':
$createForm .= $this->render('../elements/fields/upload');
break;
case 'datefield':
$createForm .= $this->render('../elements/fields/datefield');
break;
case 'select':
$createForm .= $this->render('../elements/fields/select');
break;
}
}
return $createForm;
}
datefield.ctp
$field = $this->form->hidden('RequestField.'.$FormField['id'].'.type',
array('value'=>'datefield'));
$required = '';
if($FormField['field_required']){
$required = ' required';
}
if(isset($data[$FormField['id']])){
$field .= $this->form->hidden('RequestField.'.$FormField['id'].'.id',
array('value'=>$dataMap[$FormField['id']]));
$field .= $this->Form->input('RequestField.'.$FormField['id'].'.value',
array('value'=>$data[$FormField['id']],'type'=>'text',
'required'=>$FormField['field_required'],
'label'=>$FormField['field_label'],'class'=>'form-control default date-picker',
'div'=>array('class'=>'form-group'.$required)));
} else {
$field .= $this->Form->input('RequestField.'.$FormField['id'].'.value',
array('type'=>'text','required'=>$FormField['field_required'],
'label'=>$FormField['field_label'],'class'=>'form-control default-date-picker',
'div'=>array('class'=>'form-group'.$required)));
}
echo $field;
我试过用以下方法解决它:
$view = new View($this, false);
$view->set(compact('foo', 'bar')); // set variables
$view->viewPath = 'elements'; // render an element
$html = $view->render('message');
在您的代码中,您正在引用文件夹:
app/View/elements
什么时候应该
app/View/Elements
第一行适用于 Windows(因为它使用 case-insensitive 文件和目录命名约定),但不适用于 Unix-like 操作系统。
尝试将您的代码更改为:
$this->viewPath = 'Elements';
$this->render('fields/upload');
$this->render('fields/checkbox');
//and so on