To ask you, YII2 module after the creation (such as the following forum module), the official standard is the following directory structure.
Can I create a directory manually under this module? such as the common directory, if possible to call the directory of the class file and Yii1.1 in the same?
Now you want to put the generic class of the module into that directory, just like the components directory in YII1.1, thanks.
forum/ Module.php 模块类文件 controllers/ 包含控制器类文件 DefaultController.php default 控制器类文件 models/ 包含模型类文件 views/ 包含控制器视图文件和布局文件 layouts/ 包含布局文件 default/ 包含DefaultController控制器视图文件 index.php index视图文件
Reply content:
To ask you, YII2 module after the creation (such as the following forum module), the official standard is the following directory structure.
Can I create a directory manually under this module? such as the common directory, if possible to call the directory of the class file and Yii1.1 in the same?
Now you want to put the generic class of the module into that directory, just like the components directory in YII1.1, thanks.
forum/ Module.php 模块类文件 controllers/ 包含控制器类文件 DefaultController.php default 控制器类文件 models/ 包含模型类文件 views/ 包含控制器视图文件和布局文件 layouts/ 包含布局文件 default/ 包含DefaultController控制器视图文件 index.php index视图文件
No experience, but from the default structure you listed should be directly created directory should not be automatically loaded, you need to add some settings, similar to namespace = ' Common ', path = ' Forum/common ', Specifically how to do a reference to models or controllers configuration on the line.
This equivalent to add the component class, mainly the class path and parameters you added, in the Config directory main.php configuration, so you can automatically load, can be modeled after the DB Configuration connection look like:
(It should be the path to the custom class, for example, Forumcommonutil.) The namespace corresponding to the Util class can be namespace Forumcommon)
[ 'components' => [ // 使用类名注册 "cache" 组件 'cache' => 'yii\caching\ApcCache', // 使用配置数组注册 "db" 组件 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=demo', 'username' => 'root', 'password' => '', ], // 使用函数注册"search" 组件 'search' => function () { return new app\components\SolrService; }, ],]
Thank you very much.
Discover that you can solve this problem through a namespace.
For example, create a new test directory and then uniformly set a namespace for all of the class files in that directory.