Yii 2: Module class not found

7.7k views Asked by At

In the backend/config/main file, there is a reference to the module class:

'modules' => [
    'cropk' => [
        'class' => 'app\modules\cropk\CropK',
    ]
],

In the vendor/xxx/cropk directory, there is the following class CropK:

namespace app\modules\cropk;

class CropK extends \yii\base\Module {

    public function init() {
        parent::init();
    }
}

The vendor/xxx/cropk/controllers/DefaultController:

namespace app\modules\cropk\controllers;

use yii\web\Controller;

class DefaultController extends Controller {
    public function actionIndex() {
        return $this->render('index');
    }
}

But when I access the URL http://admin.cropk.dev/cropk, I get this error:

Class app\modules\cropk\CropK does not exist

Can't I put the module outside of the backend directory? How can I do that?

1

There are 1 answers

1
ScaisEdge On BEST ANSWER

Normaly the module is indicated in this way

'modules' => [
    'moduleName' => [
        'class' => 'vendor\vendorName\moduleName\Module',

and rename your module class in Module and not Cropk

This is a sample of Module.php

    /*
     *
     *  */

    namespace vendor\xxx\modulename;

    use \yii\base\Module as BaseModule;

    /**
     *
     */
    class Module extends BaseModule
    {
        public $controllerNamespace = 'vendor\xxx\modulename\controllers';

        const VERSION = '1.0.0-dev';

        public function init()
        {
            parent::init();

            // custom initialization code goes here
        }
    }