UserController.ph

I tried to create user controller using crud, it’s created but when I try to access my view I get an error message

Unable to find ‘frontend\controllers\UserController’ in file: I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii26a/frontend/controllers/UserController.php. Namespace missing?

and the file I have it on frontend\controllers\UserController

can you paste the UserController.php’s code here


<?php


namespace app\controllers;


use Yii;

use app\models\User;

use app\models\UserSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;


/**

 * UserController implements the CRUD actions for User model.

 */

class UserController extends Controller

{

    /**

     * @inheritdoc

     */

    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['POST'],

                ],

            ],

        ];

    }


    /**

     * Lists all User models.

     * @return mixed

     */

    public function actionIndex()

    {

        $searchModel = new UserSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }


    /**

     * Displays a single User model.

     * @param integer $id

     * @return mixed

     */

    public function actionView($id)

    {

        return $this->render('view', [

            'model' => $this->findModel($id),

        ]);

    }


    /**

     * Creates a new User model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    public function actionCreate()

    {

        $model = new User();


        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 User 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 User 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 User model based on its primary key value.

     * If the model is not found, a 404 HTTP exception will be thrown.

     * @param integer $id

     * @return User the loaded model

     * @throws NotFoundHttpException if the model cannot be found

     */

    protected function findModel($id)

    {

        if (($model = User::findOne($id)) !== null) {

            return $model;

        } else {

            throw new NotFoundHttpException('The requested page does not exist.');

        }

    }

}



Namespace missing?

but it’s generated by CRUD, using gii; In crud I can’t generate using frontend I have to use app

if I use frontend I get this error message : Class ‘frontend\models\User’ does not exist or has syntax error.

But if I use app it generated

What’s “frontend”? Where do you want to use that word? Your app seems to run without any problem if you don’t insist using that word.

Where and when do you get this error?

when I try to generate a crud for user if I use : frontend\models\User

I get an error so I have to use app, and when I generate crud for user using app\models\User,

it’s generated but if I try to access user page I get the error

"Unknown Class – yii\base\UnknownClassException

Unable to find ‘frontend\controllers\UserController’ in file: I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii26a/frontend/controllers/UserController.php. Namespace missing?"

I did some pictures

Would you please show us the followings?

  1. directory structure of the app,

  2. your configuration of the web server, and

  3. the url manager settings

I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii26a\console\config\main.php


<?php

$params = array_merge(

    require __DIR__ . '/../../common/config/params.php',

    require __DIR__ . '/../../common/config/params-local.php',

    require __DIR__ . '/params.php',

    require __DIR__ . '/params-local.php'

);


return [

    'id' => 'app-console',

    'basePath' => dirname(__DIR__),

    'bootstrap' => ['log'],

    'controllerNamespace' => 'console\controllers',

    'aliases' => [

        '@bower' => '@vendor/bower-asset',

        '@npm'   => '@vendor/npm-asset',

    ],

    'controllerMap' => [

        'fixture' => [

            'class' => 'yii\console\controllers\FixtureController',

            'namespace' => 'common\fixtures',

          ],

    ],

    'components' => [

        'log' => [

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

    ],

    'params' => $params,

];



I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii26a\backend\config\main.php


<?php

$params = array_merge(

    require __DIR__ . '/../../common/config/params.php',

    require __DIR__ . '/../../common/config/params-local.php',

    require __DIR__ . '/params.php',

    require __DIR__ . '/params-local.php'

);


return [

    'id' => 'app-backend',

    'basePath' => dirname(__DIR__),

    'controllerNamespace' => 'backend\controllers',

    'bootstrap' => ['log'],

    'modules' => [],

    'components' => [

        'request' => [

            'csrfParam' => '_csrf-backend',

        ],

        'user' => [

            'identityClass' => 'common\models\User',

            'enableAutoLogin' => true,

            'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],

        ],

        'session' => [

            // this is the name of the session cookie used for login on the backend

            'name' => 'advanced-backend',

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'errorHandler' => [

            'errorAction' => 'site/error',

        ],

        /*

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'rules' => [

            ],

        ],

        */

    ],

    'params' => $params,

];



I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii26a\frontend\config\main.php


<?php

$params = array_merge(

    require __DIR__ . '/../../common/config/params.php',

    require __DIR__ . '/../../common/config/params-local.php',

    require __DIR__ . '/params.php',

    require __DIR__ . '/params-local.php'

);


return [

    'id' => 'app-frontend',

    'basePath' => dirname(__DIR__),

    'bootstrap' => ['log'],

    'controllerNamespace' => 'frontend\controllers',

    'components' => [

        'request' => [

            'csrfParam' => '_csrf-frontend',

        ],

        'user' => [

            'identityClass' => 'common\models\User',

            'enableAutoLogin' => true,

            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],

        ],

        'session' => [

            // this is the name of the session cookie used for login on the frontend

            'name' => 'advanced-frontend',

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'errorHandler' => [

            'errorAction' => 'site/error',

        ],

        /*

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'rules' => [

            ],

        ],

        */

    ],

    'params' => $params,

];



And what configurations do you have for frontend and backend in your web server?

BTW, didn’t you run “Gii” in the frontend and tried the CRUDs in the backend?

And is "app\models\User" different from "common\models\User"?

your namespaces are wrong gii does allow you to specify the namespace, what you need to change is your namespace here is your code modified


<?php


namespace frontend\controllers;


use Yii;

use app\models\User;

use app\models\UserSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;


/**

 * UserController implements the CRUD actions for User model.

 */

class UserController extends Controller

{

//...



I run CRUd on frontend not on the backend, After I changed the namespace frontend\controllers; on my controller it’s working

but why CRUD don’t accept frontend or backend when I try to generate Controllers so I have to change the namespace on every Controller

Your screenshot shows the model generator.

As @alrazi says, you should be able to specify the controller namespace with the 3rd field of the CRUD generator.

And if "app\models\User" and "common\models\User" share the same table, you should not create "app\models\User". You could specify "common\models\User" in the CRUD generator.

THANK YOUUUUUUUUUUUUU :)