Home  >  Article  >  PHP Framework  >  How to configure thinkphp to configure multiple applications and multiple configurations

How to configure thinkphp to configure multiple applications and multiple configurations

尚
forward
2020-05-07 09:25:252710browse

How to configure thinkphp to configure multiple applications and multiple configurations

Multiple modules

In ThinkPHP 3.2.3, the default application directory is ./Application, and the following default module is the Home module , if you need to add an Admin module for background application at this time, add in the default entry file ./index.php:

// 绑定Admin模块到当前入口文件
define('BIND_MODULE','Admin');

Run http://serverNmae/index.php at this time, it will be in. An Admin module is generated in the /Application directory. However, when accessing http://serverName/index.php at this time, what is actually accessed is the newly added Admin module. Even if

//设置默认模块
'DEFAULT_MODULE'    =>  'Home'

is added to ./Application/Common/Conf/config.php, it cannot be correct. Locate the Home module.

In fact, the actual meaning of defining BIND_MODULE in the entry file mentioned in the manual is to define the default module. See: ./ThinkPHP/Library/Think/Dispatcher.calss.php. This file defines ThinkPHP’s built-in Dispatcher class, which is used to complete URL parsing, routing and scheduling (see the "System Process" section in the manual), where Line :140

// 获取模块名称
define('MODULE_NAME', defined('BIND_MODULE')? BIND_MODULE : self::getModule($varModule));

In the static method dispatch, the module name will be obtained by first querying whether BIND_MODULE is defined in the entry file. If it is defined, the value of MODULE_NAME will be the value of the defined BIND_MODULE, otherwise the call will be made. Static private method getModule in the class to get the actual module name:

/**
     * 获得实际的模块名称
     */
    static private function getModule($var) {
        $module   = (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_MODULE'));
        unset($_GET[$var]);
        if($maps = C('URL_MODULE_MAP')) {
            if(isset($maps[strtolower($module)])) {
                // 记录当前别名
                define('MODULE_ALIAS',strtolower($module));
                // 获取实际的模块名
                return   ucfirst($maps[MODULE_ALIAS]);
            }elseif(array_search(strtolower($module),$maps)){
                // 禁止访问原始模块
                return   '';
            }
        }
        return strip_tags(ucfirst(strtolower($module)));
    }

In this method, if the URL does not contain the VAR_MODULE redefined by the configuration file (default is m, in ./ThinkPHP/Conf/convention. php), look for the value of DEFAULT_MODULE defined in the configuration file.

Through the above analysis, it can be concluded that BIND_MODULE actually defines the default module. If there are multiple modules in the project, do not configure it like this.

If you comment out BIND_MODULE in the default entry file ./index.php at this time (that is, use the default entry file configuration), then you can access the Admin module by directly accessing http://serverName/admin, because in this In the entry file, the application directory ./Application is defined, so accessing http://serverName/admin actually accesses the index method in ./Application/Admin/Controller/IndexController.class.php.

ThinkPHP 3.2.3 It is enough to configure multiple modules in this way. There is no need to define it in the entry file and configuration file. This is also the grouping mode officially recommended by ThinkPHP.

Another configuration is a multi-entry design, that is, create admin.php at the same level as the default entry file index.php, and add in index.php:

// 绑定Home模块到当前入口文件
define('BIND_MODULE','Home');

In admin.php Use the same configuration as index.php, except for the definition of BIND_MODULE, change the definition of BIND_MODULE to:

// 绑定Admin模块到当前入口文件
define('BIND_MODULE','Admin');

Then add: ## in the application configuration file ./Application/Common/Conf/config.php #

//设置默认模块
'DEFAULT_MODULE'    =>  'Home',

At this time, you can access the Home module by accessing http://serverName/index.php, and you can access the Admin module by accessing http://servername/admin.php, but you cannot access http://serverName/admin. , because index.php can only access the Home module at this time.

Multiple Applications

Usually ThinkPHP 3.2.3 does not need to use multi-application mode, because in most cases it can be achieved through multi-modular and multi-entry design Solve application expansion needs.

If you must use the multi-application mode, such as creating the application Application_API, you can create the directory Applicaiton_API under the same level directory as ./Application, and add the entry file ./api.php to point the application directory to ./Application_API. :

// 定义应用目录
define('APP_PATH','./Application_API/');

Note that newly added applications must have a Home module initially, even if it is set in ./Application_API/Common/Conf/config.php

//设置默认模块
'DEFAULT_MODULE'    =>  'Api',

Maybe there must be a Home module initially module, otherwise an error will be reported: Unable to load Index controller.

Recommended tutorial: "

TP5"

The above is the detailed content of How to configure thinkphp to configure multiple applications and multiple configurations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete