System multi-application mode


Multi-application

After default installation, single application mode is used for deployment. The directory structure is as follows:

├─app application directory

│ ├─controller                                                                                                

Class library Catalog

│ C ─ Public web directory (external access directory)

# ─nDex.php entrance file

# ─ ─ Router.php Quick test file

# ─.htaccess Rewriting for apache

│ 图 图 ─ View View Catalog

├ ─CONFIG Application Configuration Catalog

├ ─route route definition directory

├ ─ Runtime application runs when running The advantage of the application mode is that it is simple and flexible, and the URL address is completely controllable through routing. With the routing grouping function, a flexible mechanism similar to multiple applications can be realized.

If you want to use multi-application mode, you need to install the multi-application mode extension think-multi-app.

composer require topthink/think-multi-app

Then your application directory structure needs to be adjusted as follows (the main difference is in the app directory).

├─app application directory

│ ├─index                                                                                                                                                                                                                                                                       ​ 

│ │ ├─view         View directory

│           ├─config           Configuration directory (priority)

│ │ └─ ...       More class library directories

│ ├─admin                                                                                                                                                                                                                                                                                            

View directory

│ │ ├─config Configuration directory (priority)

│ │ └─ ... │ More class library directories

│ ├─public                WEB directory (external access directory)

│ ├─admin.php │ Backend entry file

│ ├─index.php │ Entry file

│ ├─router.php │ Quick test file

│ └─ .htaccess for apache rewriting

│ 应用—CONFIG Application Configuration Directory

─ ─ DEDEX INDEX Application configuration

# │ └ ─ ADMIN Admin Application configuration

│ ├─view                 View Directory                                                                                                                   view directory                                                                                                                   view                                          . ├─route                                                                                                                                                                                                                                                            . ├─runtime                                                                                                                                                                                  using   using   using   using using           using                 through     through through using through off     through through through off ’s ’ through ’'s ’ through ‐ to ‐ ‐‐ ‐‐ ‐ to Each application remains relatively independent and can support multiple entry files. The application can also maintain controller groups through multi-level controllers.

Automatic multi-application deployment

Supports access to multiple applications in the same entry file, and supports application mapping and customization. If you access through the index.php entry file and do not set an application name, the system automatically adopts the automatic multi-application mode.

The URL address of the automatic multi-application mode is used by default

// 访问admin应用
http://serverName/index.php/admin
// 访问shop应用
http://serverName/index.php/shop

That is to say, the first parameter of the pathinfo address represents the current application name, followed by the route or controller of the application/ operate.

If you access

http://serverName/index.php

directly, you are actually accessing the index default application. You can specify the default application through the default_app configuration parameter of the app.php configuration file.

// 设置默认应用名称
'default_app' => 'home',

Then visit

http://serverName/index.php

In fact, the home application is accessed.

In automatic multi-application mode, routing is independent for each application, so you cannot omit application parameters in the URL. But it can be solved using domain name binding.

Multi-application intelligent identification

If there is no binding entry or domain name, the application in the URL does not exist, for example, visit:

http://serverName/index.php/think

Assuming that there is no think application, the system will automatically switch to single application mode at this time. If there is a global route defined, route matching will also be checked.

If we define it in the route/route.php global route:

Route::get('think', function () {
    return 'hello,ThinkPHP!';
});

Accessing the above URL will output

hello,ThinkPHP!

If you want the think application to not exist, access it directly The routing of the default application can be configured in app.php

// 开启应用快速访问
'app_express'    =>    true,
// 默认应用
'default_app'    =>    'home',

At this time, the routing under the home application will be accessed.

Add application entry

Allows the creation of separate entry files for each application without accessing multiple applications through the index.php entry file, such as creating an admin.php Entry file to access the admin application.

// [ 应用入口文件 ]
namespace think;

require __DIR__ . '/../vendor/autoload.php';

// 执行HTTP应用并响应
$http = (new  App())->http;
$response = $http->run();
$response->send();
$http->end($response);

When multiple applications use different entries, the content of each entry file is the same. The default entry file name (without suffix) is the application name.

Use the following method to access the admin application

http://serverName/admin.php

If your entry file name is inconsistent with the application, for example, in your background admin application, the entry file name uses test.php, then the entry file needs to be changed Into:

// [ 应用入口文件 ]
namespace think;

require __DIR__ . '/../vendor/autoload.php';

// 执行HTTP应用并响应
$http = (new  App())->http;
$response = $http->name('admin')->run();
$response->send();
$http->end($response);

Get the current application

If you need to get the current application name, you can use the

app('http')->getName();

application directory to get it

Single application and multi-application modes will affect the values ​​of some system paths. In order to better understand the contents of this manual, you may need to understand the locations represented by the following system paths.

Directory location
Directory description
Getting method (helper function)
Root directoryThe directory where the project is located is automatically obtained by default and can be passed in when the entry file instantiates the App class. root_path()
Basic directory
The app directory under the root directorybase_path()
Application directoryThe directory where the current application is located. If it is a single application mode, it is the same as the basic directory. If it is a multi-application mode, it is the app/application subdirectoryapp_path()
Configuration directoryThe config directory under the root directoryconfig_path()
Runtime directoryThe directory when the framework is runtime. The single application mode is the runtime directory of the root directory, and the multi-application mode is the runtime/application subdirectoryruntime_path( )

Note: The application supports the use of the composer package. At this time, the directory may be the directory where the composer package's class library is located.

For non-automatic multi-application deployment, if you want to load the composer application, you need to set the application path in the entry file:

// [ 应用入口文件 ]
namespace think;

require __DIR__ . '/../vendor/autoload.php';

// 执行HTTP应用并响应
$http = (new  App())->http;
$response = $http->path('path/to/app')->run();
$response->send();
$http->end($response);

Application mapping

In automatic multi-application mode, application alias mapping is supported, for example:

'app_map' => [
    'think'  =>  'admin',  // 把admin应用映射为think
],

After application mapping, the original application name will not be accessible. For example, the above admin application cannot be accessed directly and can only be accessed through the think application.

Application mapping supports pan-analysis, for example:

'app_map' => [
    'think' =>  'admin',  
    'home'  =>  'index',  
    '*'     =>  'index',  
],

means that if the application accessed by the URL is not in the currently set mapping, it will be automatically mapped to the index application.

If you want to use composer to load applications, you need to set up

'app_map'    =>    [
    'think' => function($app) {
        $app->path('path/to/composer/app');
    },
],

Domain name binding application

If your multi-application uses multiple subdomain names Or for independent domain name access, you can define the binding between the domain name and the application in the config/app.php configuration file.

'domain_bind' => [
    'blog'        =>  'blog',  //  blog子域名绑定到blog应用
    'shop.tp.com' =>  'shop',  //  完整域名绑定
    '*'           =>  'home', // 二级泛域名绑定到home应用
],

Prohibit application access

If you do not want an application to be accessed through a URL, for example, you add a common subdirectory to place some public Class library, you can set

'deny_app_list' =>    ['common']