Thinkphp Learning Note 5-Modular design

Source: Internet
Author: User

Original: thinkphp Study Note 5-Modular design

1. Module structure  

The complete thinkphp is designed around the module/controller/operation and supports multiple Ingress file box multistage control. thinkphp default pathinfo mode, as follows:

http://servername/index.php (or other application portal file)/module/controller/operation/[parameter name/Parameters value ...]

Application: A project accessed by the same portal file is called an application, and in the full version of the code is application

Module: An application can contain multiple modules, each module corresponding to a separate directory, in the full version of the code has admin,home,common,runtime four modules

Controller: There are multiple controllers under each module, placed in the controller directory, a separate PHP class file

Operation: In the controller can contain more than one operation method, but also the east of an operation class, each operation is a URL to access the smallest unit, this is a bit confusing, the class is not a method, can have a return value?

2. Module design

The directory structure of the module is as follows:

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

module is not fixed, can be easily removed add, but generally common,runtime two without deleting.

Inside the module can also include configuration, controller, model and other directories, the structure is as follows:

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

In addition to the common and conf directories, others can be added and removed flexibly.

The common module is a public module that will first load the configuration file conf/config.php and common function files under this public module before accessing all modules common/ function.php, but the function.php file is not found, the common module itself cannot be accessed directly via a URL, and other files in the public module can be integrated or invoked by other modules. In addition, the location of the public module can be changed by Common_path constant, and the Common_path is redefined in the portal file as follows:

Define (' Common_path ', './common/');
Define (' App_path ', './application/');
require './thinkphp/thinkphp.php ';

The directory structure changes as follows

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

I am not here with define (' Common_path ', './common/'); generated, but define (' Common_path ', ' common/'); The resulting directory is as follows:

So the common directory under the application directory will not work.

3. Automatically generate module catalog

3.2.2 version can be automatically generated by the default module and automatically generated controllers and models, for example, now to generate an admin module for site management, you can add the following code under the portal file

// binding the Admin module to the current portal file Define (' Bind_module ', ' Admin '); Define (' App_path ', './application/'); require './thinkphp/thinkphp.php ';

Then refresh to generate an admin directory under the application directory, And in the Controller directory to generate a default controller IndexController.Class.php file, the note in the manual is bound to the Admin module to the current entry file, in fact, the specific role of this sentence is: if there is no application below the admin this module automatically generate this module and Take this module as a portal file, note if the binding is the home module, and access to the Admin directory will be error, Http://Servername/thinkphp/index.php/Admin/Index, the wrong content is:

Unable to load controller: Admin

Also, if we delete this binding after generating the module, there is no mistake, so we can easily access any module.

After building the module we can also automatically generate the controller and model files within the module, the code is as follows:

Define (' Bind_module ', ' Admin '); Define (' Build_controller_list ', ' index,user,menu ');

Note that these two sentences to execute together, I test found that if you start to execute the first sentence of the building module, and then the next sentence to build the controller is not successful, to delete the admin directory, and then execute, this will generate the directory and then the Controller directory to generate the controllers file, this is a bit of a fool.

The generated controller is inherited from Think\controller. You can also use the command line to generate the controller, here I repeat.

The same can be automatically generated by the model, the code is as follows:

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 ';

Similarly, there can be no order, that is to say that the three sentences must be executed together to be effective, can not be executed one by one. Controller inherits from Think\model

As far as I can see, if there is no binding module it will automatically generate the home module and access the module

The system default configuration is to prohibit access to the common module and runtime module, we can also add our own block access to the module, the code is as follows:

// set the list of forbidden Modules ' module_deny_list ' + =  array(' Common ', ' Runtime ', ' Api '),

You can also set accessible modules and default modules, as follows:

' module_allow_list '    = =    array(' home ', ' Admin ', ' User '), ' default_module '       = '    home ',

As far as I can see, thinkphp also generates and accesses the home module by default, even if the default module is not set.

If the project is relatively simple, you can also set up a single module, the code is as follows:

// Turn off multi-module access ' Multi_module ' and  false, ' default_module '        = '  Home ',

The same use and module can be used to bind multiple portals, different entry files to set different application modes and binding modules, the code is as follows:

// bind home module to current portal file $_get [' m '] = ' Home 'define(' App_path ', './application/'); require './thinkphp/thinkphp.php ';

This allows you to use the following path to access the Http://Servername/thinkphp/home.php/Index

The content in this home.php is the same as the index.php content, and is also a portal file.

Thinkphp Learning Note 5-Modular design

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.