ThinkPHP5.0 framework module design details, thinkphp5.0 details

Source: Internet
Author: User
Tags php database

ThinkPHP5.0 framework module design details, thinkphp5.0 details

This article describes the thinkPHP5.0 framework module design. We will share this with you for your reference. The details are as follows:

In version 5.0, the module functions are flexibly designed. By default, the multi-module architecture is used, and the single module design is supported, the namespaces of all modules use the app as the root namespace (configuration changes are allowed ).

Directory structure

The standard application and Module Directory structure is as follows:

‑Application application directory (configurable)
│ Catalog-common public Module Directory (optional)
│ ├ ── Common. php public function File
│ Route-route. php route configuration file
│ ├ ── Database. php database Configuration File
│ ├ ── Config. php application configuration file
│ ├ ── Module1 Module 1 directory
│ ├ ── Config. php module configuration file
│ Functions-common. php module function File
│ ─-Controller directory
│ ├-Model directory
│ Preview-view directory
│ ─ ──... More class library Directories

│ ├ ── Module2 module 2 Directory
│ ├ ── Config. php module configuration file
│ Functions-common. php module function File
│ ─-Controller directory
│ ├-Model directory
│ Preview-view directory
│ ─ ──... More class library Directories

According to the ThinkPHP5.0 naming rules, all modules are named in lower case and underline.

Module name avoid using PHP reserved keywords (for a list of reserved words, see the http://php.net/manual/zh/reserved.keywords.php), otherwise system errors may occur.

The common module is a special module and direct access is prohibited by default. It is generally used to place some public class libraries for inheritance of other modules.

Module class library

The namespace of the class library file under a module must start with the name of app \ module. For example:

// Index controller class of the Index module app \ index \ controller \ Index // User model class of the index module app \ index \ model \ User

Here, the app can be changed by definition. For example, we can modify it in the application configuration file:

'app_namespace' => 'application',

Then, the namespace of the index module becomes:

// Index controller class of the Index module application \ index \ controller \ Index // User model class of the index module application \ index \ model \ User

For more information about the relationships between class libraries and namespaces, refer to the previous section "thinkPHP5.0 framework namespace details".

Module and controller hiding

Because multiple modules are supported by default, the current module must be identified in the URL address when multiple modules exist. If only one module exists, bind the module, the method is to add the following code to the public file of the application:

// Bind the current access to the index module \ think \ Route: bind ('index ');

After binding, our URL access address becomes:

Http: // serverName/index. php/controller/Operation/[parameter name/parameter value...]

The accessed module is the index module.

If your application is simple and there is only one module and one controller, You can bind the module and controller to the public file of the application, as shown below:

// Bind the index controller \ think \ Route: bind ('index/Index') currently accessed to the index module ');

After setting, our URL access address is changed:

Http: // serverName/Application entry/Operation/[parameter name/parameter value...]

The accessed module is the index module, and the Controller is the Index controller.

Single Module

If your application is relatively simple and has only one module, You can further simplify it into using a single module structure. The method is as follows:

First, define in the application configuration file:

// Disable multi-module design 'app _ multi_module '=> false,

Then, adjust the application directory structure as follows:

‑Application application directory (configurable)
│ ─-Controller directory
│ ├-Model directory
│ Overview-view directory
│ ├ ──... More class library Directories
│ Functions-common. php function File
│ Route-route. php route configuration file
│ ├ ── Database. php database Configuration File
│ └ ── Config. php configuration file

The URL access address is changed

Http: // serverName/index. php (or other application portal)/controller/Operation/[parameter name/parameter value...]

At the same time, The namespace of the application class library under the design of a single module is also adjusted, for example:

Original

app\index\controller\Indexapp\index\model\User

Change

app\controller\Indexapp\model\User

More URL simplification and customization can also be achieved through the URL routing function.

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.