Galilo Galilo
Galilo Galilo

Reputation: 569

yii2 Search model sometimes not found

enter image description hereI am using yii2 basic app. On local host, my site was running perfect, when I turned to another computer there is a problem with the site, where after login to the admin panel, the user is redirected to ..../breaking-news/index. The problem is that on one computer (local host) it is running perfect, on the other computer (local host or on the internet) it worked perfect for a while then while trying to test after some hours, it gives me the following error after login:

Error

Class 'app\controllers\app\models\appModels\BreakingNewsSearch' not found

my controller is:

    <?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.');
        }
    }

}

The search model is here: ...\mywebsite\models\appmodels\BreakingNewsSearch.php

And the error is : class app\models\appModels\BreakingNewsSearch not found

Upvotes: 0

Views: 1376

Answers (2)

Galilo Galilo
Galilo Galilo

Reputation: 569

As the others said, it was a problem in models/appModels/... The correct file path is models/appmodels/... The error was not observable on windows, but on ubuntu it resulted in the mentioned issue. Thanks for all of you...

Upvotes: 0

Pedro del Sol
Pedro del Sol

Reputation: 2841

assuming your use statement is correct, ie

use app\models\appModels\BreakingNewsSearch;

would include your BreakingNewsSearch, then you can create a new instance without the qualified name.

/**
 * 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,
    ]);
}

If yii still can't autoload the BreakingNewsSearch class then your path is wrong; try

use app\models\appModels\BreakingNewsSearch;

to match

 use app\models\appmodels\AppBreakingNews;

Upvotes: 2

Related Questions