YII2 Module Custom Module Catalog

Source: Internet
Author: User
Keywords Php yii2
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.