yii2有时找不到搜索模型

时间:2017-04-28 14:40:36

标签: php yii2

enter image description here我正在使用yii2基本应用。在本地主机上,我的网站运行正常,当我转向另一台计算机时,网站出现问题,登录管理面板后,用户被重定向到.... / breaking-news / index。 问题是,在一台计算机(本地主机)上,它运行完美,在另一台计算机(本地主机或互联网上)上工作了一段时间,然后在几小时后尝试测试时,它给了我以下错误登录:

  

错误

     

Class' app \ controllers \ app \ models \ appModels \ BreakingNewsSearch'不   结果

我的控制器是:

    <?php

namespace app\controllers;

    use app\models\appmodels\AppBreakingNews;
    use app\models\appModels\BreakingNewsSearch;
    use Yii;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\helpers\Url;
    use yii\web\NotFoundHttpException;
    use yii\web\Response;

    /**
     * BreakingNewsController implements the CRUD actions for AppBreakingNews model.
     */

    class BreakingNewsController extends BEController {

        public function behaviors() {
            return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index', 'view', 'create', 'update', 'delete', 'find-model'],
                'rules' => [
                    [
                        'allow' => TRUE,
                        'actions' => [ 'index', 'view', 'create', 'update', 'delete', 'find-model'],
                        'roles' => ['@'],
                    ],
                    [
                        'allow' => FALSE,
                        'actions' => ['index', 'view', 'create', 'update', 'delete', 'find-model'],
                        'roles' => ['?'],
                    ],
                ],
                'denyCallback' => function ($rule, $action) {
            return $this->redirect(Url::toRoute(['site/index']));
        }
            ],
        ];
    }

    public function actionGetMainNews() {
        if (Yii::$app->request->isAjax) {
            $data = Yii::$app->request->post();

            $news = AppBreakingNews::find()->all();

            Yii::$app->response->format = Response::FORMAT_JSON;
            return [
                'data' => $news,
            ];
        }
    }

    /**
     * Lists all AppBreakingNews models.
     * @return mixed
     */
    public function actionIndex() {
`//        $searchMod`el = new BreakingNewsSearch();
        $searchModel = new app\models\appModels\BreakingNewsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single AppBreakingNews model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new AppBreakingNews model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new AppBreakingNews();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing AppBreakingNews model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing AppBreakingNews model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the AppBreakingNews model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return AppBreakingNews the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = AppBreakingNews::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}

搜索模型在这里:... \ mywebsite \ models \ appmodels \ BreakingNewsSearch.php

错误是:找不到类app \ models \ appModels \ BreakingNewsSearch

2 个答案:

答案 0 :(得分:2)

假设您的use语句正确无误,即

use app\models\appModels\BreakingNewsSearch;

将包含您的BreakingNewsSearch,然后您可以创建没有限定名称的新实例。

/**
 * Lists all AppBreakingNews models.
 * @return mixed
 */
public function actionIndex() {
    $searchModel = new BreakingNewsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
    ]);
}

如果yii仍然无法自动加载BreakingNewsSearch类,那么你的路径是错误的;试试

use app\models\appModels\BreakingNewsSearch;

匹配

 use app\models\appmodels\AppBreakingNews;

答案 1 :(得分:0)

正如其他人所说,这是模特/ appModels /中的一个问题...... 正确的文件路径是models / appmodels / ... 该错误在Windows上无法观察到,但在ubuntu上导致了上述问题。 谢谢你们所有人......