ThinkPHP3.2.3 Adding a new module process

Automatically generate module catalogs
Starting from version 3.2.2, it can support automatic generation of module catalogs other than the default modules and batch generation of controllers and model classes.
For example, if we need to generate an Admin module for background applications, define it as follows in the application entry file:

// Bind the Admin module to the current entry file
define('BIND_MODULE','Admin');
define('APP_PATH','./Application/');
require'./ThinkPHP / ThinkPHP.php';

Then visit the URL address

http://serverName/index.php

The directory of the Admin module will be generated, and a default controller class Admin\Controller\IndexController will be generated. If you need to generate more controller classes, you can define the BUILD_CONTROLLER_LIST constant, for example:

// Bind the Admin module to the current entry file
define('BIND_MODULE','Admin');
define('BUILD_CONTROLLER_LIST','Index,User,Menu');
define('APP_PATH','./Application/');
require'./ThinkPHP/ThinkPHP.php';

Three specified controller classes will be automatically generated after access:

Admin\Controller\IndexController
Admin\Controller\UserController
Admin\Controller\MenuController

Note: The controller classes generated by default inherit Think\Controller. If you need to inherit other public classes, you need to adjust them separately. If APP_USE_NAMESPACE is turned off in the application's public configuration file, the generated controller class will not adopt the namespace definition.

You can also manually call the Think\Build class method to generate the controller class, for example:

// Generate the Role controller class of the Admin module
// The default class library is Admin\Controller\RoleController
// If it already exists, it will not be regenerated
\Think\Build::buildController('Admin','Role');

Similarly, you can also define BUILD_MODEL_LIST to support the generation of multiple model classes:

// 绑定Admin模块到当前入口文件
define(‘BIND_MODULE’,‘Admin’);
define(‘BUILD_CONTROLLER_LIST’,‘Index,User,Menu’);
define(‘BUILD_MODEL_LIST’,‘User,Menu’);
define(‘APP_PATH’,’./Application/’);
require ‘./ThinkPHP/ThinkPHP.php’;

Access will automatically generate model classes:

Admin\Model\UserModel
Admin\Model\MenuModel

Note: The model classes generated by default inherit Think\Model. If you need to inherit public model classes, you need to adjust them separately. If APP_USE_NAMESPACE is turned off in the public configuration file of the application, the generated model class will not adopt the namespace definition.

You can also manually call the Think\Build class method to generate the model class, for example:

// Generate the Role model class of the Admin module
// The default class library is Admin\Model\RoleModel
// If it already exists, it will not be regenerated
\Think\Build::buildModel('Admin','Role');

Block access to modules
3.2 Access to the module is automatically judged, so usually you can access it without configuring the module list, but you can configure the list of forbidden modules (used to be called by other modules or not open for access). The default configuration is Forbid access to Common module and Runtime module (Runtime directory is the default runtime directory), we can add other list of prohibited access modules:

// Set the list of prohibited
modules'MODULE_DENY_LIST' => array('Common','Runtime','Api'),

After setting, the Api module cannot be directly accessed through URL. In fact, we may just place some public interface files under the module, so it is only necessary to call it internally.

Set the access list
If your application has fewer modules, you can also set the allowed access list and the default module, which can simplify the URL access of the default module.

‘MODULE_ALLOW_LIST’ => array(‘Home’,‘Admin’,‘User’),
‘DEFAULT_MODULE’ => ‘Home’,

After setting, modules other than the Home, Admin and User modules cannot be accessed directly, and the Home module is the default access module (it may not appear in the URL address).

Guess you like

Origin blog.csdn.net/minihuabei/article/details/112471186