控制器无法找到请求的视图Yii错误

时间:2014-05-03 07:18:28

标签: php view yii controller

我正在研究Yii项目。它在我的localhost上完美运行。当我在服务器上传项目时,我在产品csv上传页面上在服务器上出现错误并且它给了我错误

ImportcsvController找不到请求的视图“import”。

我在视图文件夹和Importcsv模型文件中有Importcsv Controller文件,Importcsv文件夹(包含import.php和importcsv.php)。在localhost上一切正常,但我不明白为什么它在服务器上给我错误。所有文件都完美上传。

我的控制器

<?php
class ImportcsvController extends Controller
{
/**
 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
 * using two-column layout. See 'protected/views/layouts/column2.php'.
 */
public $layout='//layouts/main';
public $defaultAction='import';

/**
 * @return array action filters
 */
public function filters()
{
    return array(
        'accessControl', // perform access control for CRUD operations
        'postOnly + delete', // we only allow deletion via POST request
    );
}

/**
 * Specifies the access control rules.
 * This method is used by the 'accessControl' filter.
 * @return array access control rules
 */
public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('view','import'),
            'users'=>array('*'),
        ),

    );
}

/**
 * Displays a particular model.
 * @param integer $id the ID of the model to be displayed
 */
public function actionView($id)
{
    $this->render('view',array(
        'model'=>$this->loadModel($id),
    ));
}



    public function actionImport() {

        $model = new Importcsv;
        $file = CUploadedFile::getInstance($model,'csv_file');
        if(isset($_POST['Importcsv']))
        {
            $model->attributes=$_POST['Importcsv'];
            if(!empty($_FILES['Importcsv']['tmp_name']['csv_file']))
            {
                $file = CUploadedFile::getInstance($model,'csv_file');


                $fp = fopen($file->tempName, 'r');
                if($fp)
                {
                     $line = fgetcsv($fp, 1000, ",");

                    $first_time = true;


                                            //begin transaction
                                            $transaction=Yii::app()->db->beginTransaction();

                                        try {


                                            //truncate table before importing csv
                                            Yii::app()->db->createCommand("truncate table products")->execute();



                                            //add csv ecords

                                                        do {
                                                                if ($first_time == true) {
                                                                        $first_time = false;
                                                                        continue;
                                                                }
                                                                $model = new Products;

                                                                $model->product_name  = $line[1];
                                                                $model->product_code  = $line[2];
                                                                $model->price  = $line[3];
                                                                $model->mrp  = $line[4];
                                                                $model->quantity  = $line[5];
                                                                if($line[6]==''){ 
                                                                        $model->image  = 'noimage.jpg';
                                                                }
                                                                else {
                                                                        $model->image  = $line[6];
                                                                }
                                                                $model->status  = $line[7];

                                                                $model->created_at  = date('Y-m-d h:i:s');

                                                                $model->save();
                                                        }while( ($line = fgetcsv($fp, 1000, ",")) != FALSE);

                                            $transaction->commit();
                                             } catch (Exception $e) {
                                            $transaction->rollback();
                                             }


                                    }

            }

            $this->redirect(array("Products/index"));

        }


        $this->render('import', array('model' => $model) );

    }



/**
 * Performs the AJAX validation.
 * @param Company $model the model to be validated
 */
protected function performAjaxValidation($model)
{
    if(isset($_POST['ajax']) && $_POST['ajax']==='importcsv-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
}
}

?>

我的观点是(import.php)

<?php
 // var_dump($model);
 $this->renderPartial('importcsv', array('model'=>$model)); 
?>

importcsv.php是

<div class="form">





<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(

'id'=>'importcsv-form',

'enableAjaxValidation'=>true,

'type' => 'horizontal',

'htmlOptions' => array('enctype'=>'multipart/form-data', 'class'=>'col-sm-offset-   2'),

));



?>



<fieldset>

    <legend>

        <p class="note">Fields with <span class="required">*</span> are required.</p>

    </legend>





    <div class="control-group">     

        <div class="span4">

                <?php echo $form->labelEx($model,'csv_file'); ?> 

                <?php echo $form->fileField($model,'csv_file'); ?> 

                <?php echo $form->error($model, 'csv_file'); ?>

        </div>

    </div>



    <div class="row buttons  col-sm-offset-2">

        <?php echo CHtml::submitButton('Upload CSV',array("class"=>"btn btn-primary")); ?> <?php echo $form->errorSummary($model); ?>

    </div>

</fieldset>



<?php $this->endWidget(); ?>

我的模型是(importcsv.php)

<?php

class Importcsv extends CFormModel
{
public $csv_file;
/**
 * @return array validation rules for model attributes.
 */
public function rules() { 
        return array( array('csv_file', 'file', 'types' => 'csv' ,
            'maxSize'=>5242880, 
            'allowEmpty' => true, 
            'wrongType'=>'Only csv allowed.', 
            'tooLarge'=>'File too large! 5MB is the limit'
            )
            );  


} 

public function attributeLabels() { 
        return array('csv_file'=>'Import CSV',); 
}

}

?>

我不知道我哪里出错了...请帮助我,并提前致谢....

1 个答案:

答案 0 :(得分:1)

在protected / views中创建的目录的第一个字母不能以大写字母开头(无论如何都在Linux系统上)。

如果您的托管服务是Linux,那么请检查文件夹名称,这可能会对您有所帮助, 所有最好的