CakePHP Controller中的元素渲染会抛出Missing View错误

时间:2016-01-17 09:06:11

标签: php cakephp cakephp-2.x

我正在尝试在控制器中呈现视图并使用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');

1 个答案:

答案 0 :(得分:1)

在您的代码中,您正在引用文件夹:

app/View/elements 

什么时候应该

app/View/Elements 

第一行适用于Windows(因为它使用不区分大小写的文件和目录命名约定),但不适用于类Unix操作系统。

尝试将代码更改为:

$this->viewPath = 'Elements';
$this->render('fields/upload');
$this->render('fields/checkbox');
//and so on