Modular design thinkphp

Based on a complete ThinkPHP application module / controller / operator design, and, if desired, can support multi-inlet and multi-level controller files.

ThinkPHP3.2 modular architecture design, specification of the directory structure has been adjusted, it can support the creation of multi-module application, so that the extended application more convenient.

A typical URL access rules (PATHINFO our default mode as an example, of course, can support normal URL pattern):

  1. http://serverName/index.php(或者其他应用入口文件)/模块/控制器/操作/[参数名/参数值...]

ThinkPHP3.2 applications can support the switch to the command-line access, if you switch to the command line mode, the following access rules:

  1. >php.exe index.php(或其它应用入口文件) 模块/控制器/操作/[参数名/参数值...]

Several of the concepts explained under:

name description
application Project-based access with an entry document we call an application.
Module The following application may comprise a plurality of modules, each module in the following application catalog is a separate subdirectory.
Controller Each module may comprise a plurality of controllers, one controller class is usually embodied as a controller.
operating Each of the controller may comprise a plurality of methods of operation, it may be bound to a class of operation, each operation is a minimum unit accessible URL.

 

The modular design of the following ideas are the most important part of the module, the module that contains the configuration file is actually a collection of files and functions MVC file (directory).

Module Design

The new modular design architecture, the following is a directory of the directory structure of the application modules, each module can be easily uninstalled and deployment, and support public module.

  1. Application 默认应用目录(可以设置)
  2. ├─Common 公共模块(不能直接访问)
  3. ├─Home 前台模块
  4. ├─Admin 后台模块
  5. ├─... 其他更多模块
  6. ├─Runtime 默认运行时目录(可以设置)

Note: version 3.2 improves on the original grouping of independent 3.1.3 on independent groups after the improvement is the new version of the module, the module before the renamed controller. By default, as long as there is a directory application directory module, the module can be accessed only when you want to prohibit certain module or modules only allow access only to the need for relevant settings module list.

Each module is relatively independent, the directory structure is as follows:

  1. ├─Module 模块目录
  2. ├─Conf 配置文件目录
  3. ├─Common 公共函数目录
  4. ├─Controller 控制器目录
  5. ├─Model 模型目录
  6. ├─Logic 逻辑目录(可选)
  7. ├─Service Service目录(可选)
  8. ... 更多分层目录可选
  9. └─View 视图目录

As a result of the multi-layer MVC mechanism, and in addition Conf Common directory, the following directory structure of each module can be flexibly set and added as required, so does not adhere to the pictures show the directory

Public module

Common module is a special module, application module is public, the following modules will be loaded first public profile before access to all modules ( Conf/config.php) public function and file ( Common/function.php). But Common module itself can not directly access the URL, other public documents modules can be inherited or called by other modules.

The position of the common module by COMMON_PATH changing the constants, we can redefine the inlet file COMMON_PATH follows:

  1. define('COMMON_PATH','./Common/');
  2. define('APP_PATH','./Application/');
  3. require './ThinkPHP/ThinkPHP.php';

Its application directory structure becomes:

  1. www WEB部署目录(或者子目录)
  2. ├─index.php 入口文件
  3. ├─README.md README文件
  4. ├─Common 应用公共模块目录
  5. ├─Application 应用模块目录
  6. ├─Public 应用资源文件目录
  7. └─ThinkPHP 框架目录

Once defined, Application directory no longer need the Common catalog.

Automatic generation module directory

From the 3.2.2 version of the beginning, it can support automatic generation module directory other than the default module and generate batch controller and model classes.

For example, if we need to generate a module for background Admin application, the application entry in the file are defined as follows:

  1. // 绑定Admin模块到当前入口文件
  2. define('BIND_MODULE','Admin');
  3. define('APP_PATH','./Application/');
  4. require './ThinkPHP/ThinkPHP.php';

Then visit the URL address

  1. http://serverName/index.php

Admin module directory will be generated, and generates a default controller class Admin\Controller\IndexController. If the controller needs to generate more classes may be defined BUILD_CONTROLLER_LISTconstants, for example:

  1. // 绑定Admin模块到当前入口文件
  2. define('BIND_MODULE','Admin');
  3. define('BUILD_CONTROLLER_LIST','Index,User,Menu');
  4. define('APP_PATH','./Application/');
  5. require './ThinkPHP/ThinkPHP.php';

It will automatically generate the three access classes designated controller:

  1. Admin\Controller\IndexController
  2. Admin\Controller\UserController
  3. Admin\Controller\MenuController

Note: The default generated controller class are inherited Think\Controller, if you need to inherit other public classes require additional adjustments. If the application is provided in the common closed profile  APP_USE_NAMESPACE, then the generated class does not use the controller namespace definition.

You may also be manually invoke their Think\Buildmethods to generate class controller class, for example:

  1. // 生成Admin模块的Role控制器类
  2. // 默认类库为Admin\Controller\RoleController
  3. // 如果已经存在则不会重新生成
  4. \Think\Build::buildController('Admin','Role');

Similarly, also define BUILD_MODEL_LISTsupports the generation of a plurality of model classes:

  1. // 绑定Admin模块到当前入口文件
  2. define('BIND_MODULE','Admin');
  3. define('BUILD_CONTROLLER_LIST','Index,User,Menu');
  4. define('BUILD_MODEL_LIST','User,Menu');
  5. define('APP_PATH','./Application/');
  6. require './ThinkPHP/ThinkPHP.php';

Access will automatically generate model classes:

  1. Admin\Model\UserModel
  2. Admin\Model\MenuModel

Note: The default generated model classes inherit Think\Model, if you need to inherit a common model class requires additional adjustment. If the application is provided in the common closed profile  APP_USE_NAMESPACE, then the generated model does not use class namespace definition.

You can also call your own manual method Think \ Build class to generate model classes, such as:

  1. // 生成Admin模块的Role模型类
  2. // 默认类库为Admin\Model\RoleModel
  3. // 如果已经存在则不会重新生成
  4. \Think\Build::buildModel('Admin','Role');

No Access Module

Access 3.2 pairs of modules is automatically judged, so usually do not need to configure the module to access the list, but the list of modules can be configured to block access (for other modules to be called or not open access), the default configuration is to prohibit access Commonmodule and a Runtimemodule (runtime runtime directory is the default directory), we can add other prohibited visit the module list:

  1. // 设置禁止访问的模块列表
  2. 'MODULE_DENY_LIST' => array('Common','Runtime','Api'),

After setting, Api module can not be accessed directly via URL, in fact, we might just put some files in the common interface module below, and therefore are internal calls can be.

Setting Access Lists

If you apply the following module is relatively small, you can also set the default module and allow access list, which can simplify the URL to access the default module.

  1. 'MODULE_ALLOW_LIST' => array('Home','Admin','User'),
  2. 'DEFAULT_MODULE' => 'Home',

After setting up, in addition to Home, Admin and User modules module not be accessed directly, and Home module is the default access module (may not appear in the URL address).

Single-module design

If your application is simple enough, then perhaps only with a module can be completed, it can be set directly:

  1. // 关闭多模块访问
  2. 'MULTI_MODULE' => false,
  3. 'DEFAULT_MODULE' => 'Home',

Once you turn off multiple access module, you can only access the default module (set here is Home).

After a single common modular design module is still valid

Multi-Entry design

Plurality of inlets may be provided to the application and the same module, different files of the inlet may be provided for different applications or modes binding module.

For example, we index.phpadd a sibling directory file home.phpentry documents, and bind the Home module:

Version 3.2.0 wording:

  1. // 绑定Home模块到当前入口文件
  2. $_GET['m'] = 'Home';
  3. define('APP_PATH','./Application/');
  4. require './ThinkPHP/ThinkPHP.php';

Version 3.2.1 above wording:

  1. // 绑定Home模块到当前入口文件
  2. define('BIND_MODULE','Home');
  3. define('APP_PATH','./Application/');
  4. require './ThinkPHP/ThinkPHP.php';

If you change the default setting variables, variables needed to be done corresponding module binding adjustments.

After binding module, the original access address

  1. http://serverName/index.php/Home/Index/index

Becomes

  1. http://serverName/home.php/Index/index

In the same way, we can also bind a controller at the entrance file, for example:

Version 3.2.0 wording:

  1. $_GET['m'] = 'Home'; // 绑定Home模块到当前入口文件
  2. $_GET['c'] = 'Index'; // 绑定Index控制器到当前入口文件
  3. define('APP_PATH','./Application/');
  4. require './ThinkPHP/ThinkPHP.php';

Version 3.2.1 above wording:

  1. define('BIND_MODULE', 'Home'); // 绑定Home模块到当前入口文件
  2. define('BIND_CONTROLLER','Index'); // 绑定Index控制器到当前入口文件
  3. define('APP_PATH','./Application/');
  4. require './ThinkPHP/ThinkPHP.php';

After binding module and the controller, the original access address:

  1. http://serverName/index.php/Home/Index/index

Becomes:

  1. http://serverName/home.php/index

Different entry file can also be used to bind a different application mode, the application mode, the reference portion.

Guess you like

Origin www.cnblogs.com/furuihua/p/11757953.html