Yii Framework Forum: UserController.ph - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

UserController.ph

#1 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 16-December 17

Posted 23 December 2017 - 03:57 AM

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

Attached File(s)


0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,546
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 23 December 2017 - 02:28 PM

can you paste the UserController.php's code here
0

#3 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 16-December 17

Posted 23 December 2017 - 03:47 PM

View Postalrazi, on 23 December 2017 - 02:28 PM, said:

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

0

#4 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 205
  • Joined: 10-October 11
  • Location:Russia

Posted 23 December 2017 - 04:29 PM

Namespace missing?
0

#5 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 16-December 17

Posted 23 December 2017 - 04:55 PM

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
0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,626
  • Joined: 16-February 11
  • Location:Japan

Posted 23 December 2017 - 06:22 PM

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.

Quote

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

Where and when do you get this error?
0

#7 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 16-December 17

Posted 23 December 2017 - 08:12 PM

View Postsoftark, on 23 December 2017 - 06:22 PM, said:

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

Attached File(s)

  • Attached File  1.png (24.27K)
    Number of downloads: 2
  • Attached File  2.png (70.63K)
    Number of downloads: 2
  • Attached File  3.png (40.19K)
    Number of downloads: 2
  • Attached File  4.png (19.25K)
    Number of downloads: 2
  • Attached File  5.png (40.17K)
    Number of downloads: 2
  • Attached File  6.png (22.13K)
    Number of downloads: 2

0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,626
  • Joined: 16-February 11
  • Location:Japan

Posted 23 December 2017 - 08:31 PM

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
0

#9 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 16-December 17

Posted 23 December 2017 - 10:55 PM

View Postsoftark, on 23 December 2017 - 08:31 PM, said:

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,
];

Attached File(s)

  • Attached File  001.png (63.21K)
    Number of downloads: 1
  • Attached File  002.png (46.84K)
    Number of downloads: 1
  • Attached File  003.png (47.28K)
    Number of downloads: 1

0

#10 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,626
  • Joined: 16-February 11
  • Location:Japan

Posted 23 December 2017 - 11:54 PM

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"?
0

#11 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,546
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 24 December 2017 - 12:13 AM

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
{
//...

0

#12 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 16-December 17

Posted 24 December 2017 - 01:17 AM

View Postsoftark, on 23 December 2017 - 11:54 PM, said:

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"?


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

Attached File(s)

  • Attached File  4.png (34.38K)
    Number of downloads: 2

0

#13 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,626
  • Joined: 16-February 11
  • Location:Japan

Posted 24 December 2017 - 03:27 AM

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.
0

#14 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 16-December 17

Posted 24 December 2017 - 08:40 PM

View Postsoftark, on 24 December 2017 - 03:27 AM, said:

You could specify "common\models\User" in the CRUD generator.


THANK YOUUUUUUUUUUUUU :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users