deepak
deepak

Reputation: 35

Namespace error in yii2 controller

I want to make a installation script for my app in yii2 and for that I want to redirect it to a defaultRoute='installation/index' but I am getting this namespace error when I have right namespace in my installation controller

Also I have a Installation model which does not extends to the activerecords and is used to get the user input values and perform some actions without the need of saving them into DB but it's directory is also not found.

Below is the error I am getting

Installation controller code:

namespace livecrm\controllers;

class InstallationController extends \yii\web\Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }

}

install-config.php:

$config = [
    'id' => 'app-livecrm',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],

    'defaultRoute' => '/installation/index',
    'components' => [
        'request' => [
            'cookieValidationKey' => 'JDqkJaMgIITAKcsJY6yvLQdM9jf7WghX',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'livefactory\models\User',
            'enableAutoLogin' => false,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
    ],
];
return $config;

config/main.php:

$params = array_merge(
    require(__DIR__ . '/../../livefactory/config/params.php'),
    require(__DIR__ . '/../../livefactory/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [



    'id' => 'app-livecrm',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'livecrm\controllers',
    'bootstrap' => ['log'],
    'modules' => [

        'gii' => [
    'class' => 'yii\gii\Module',
    'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '*'] // adjust this to your needs
],

     'gridview' => [
            'class' => 'kartik\grid\Module',
        ],

         'liveobjects' => [
            'class' => 'livefactory\modules\liveobjects\Module',
        ],
         'pmt' => [
            'class' => 'livefactory\modules\pmt\Module',
        ],
         'user' => [
            'class' => 'livefactory\modules\user\Module',
        ],
         'sales' => [
            'class' => 'livefactory\modules\sales\Module',
        ],
         'customer' => [
            'class' => 'livefactory\modules\customer\Module',
        ],
        'product' => [
            'class' => 'livefactory\modules\product\product',
        ],
        'cron' => [
            'class' => 'livefactory\modules\cron\Module',
        ],


    ],
    'components' => [
        'user' => [
            'identityClass' => 'livefactory\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'authManager'=>[
            'class' => 'yii\rbac\DbManager',
            'defaultRoles' =>['guest'],
        ],
        'as access' => [
            'class' => 'mdm\admin\components\AccessControl',
            'allowActions' => [
                'site/*', // add or remove allowed actions to this list
            ]
        ],
    ],

    'params' => $params,
];

Upvotes: 1

Views: 1808

Answers (1)

arogachev
arogachev

Reputation: 33538

Seems like you are using basic application template.

The namespace of controller for your case should be:

namespace app\controllers\InstallationController;

The error message is very clear by the way and tells exactly about that.

Update: If you need namespace different than app\controllers you can change it through controllerNamespace property of yii\base\Applcation. For example you can add this to your config:

'controllerNamespace' => 'livecrm\\controllers',

Official docs:

Upvotes: 2

Related Questions