Write here about configuring multiple applications under TP6. Because TP6 and TP5 are different, TP6 defaults to single application mode (single module), but our actual projects often have multiple applications (multiple modules), so when using TP6, you need to configure and enable multi-application mode.
Table of contents
1. Install multi-application mode
1. Install multiple application models
2. Install think-view extension
2. Delete files in the app directory
4. Configure default applications
1. Apache is modified as follows
2. nginx is modified as follows
6. Configure environment variables
1. Install multi-application mode
1. Install multiple application models
Enter the project root directory, open CMD, and use composer to install the multi-application model
composer require topthink/think-multi-app
2. Install think-view extension
In the root directory, use composer to install the think-view extension. The official manual explains this.
The view function is completed by the \think\View class together with the view driver (that is, template engine driver) class. The new version only has a built-in PHP native template engine (mainly used for built-in exception page output). If you need to use other template engines, you need to use them separately. Install the appropriate template engine extension.
If you need to use the think-template template engine, you only need to install the think-view template engine driver.
composer require topthink/think-view
If the extension is not installed, an error will be reported when using "return view()" in the controller.
2. Delete files in the app directory
Delete all the files that come with the app directory. Because multiple applications need to be re-created, the original ones are no longer needed and can be deleted.
3. Create a new application
Create a new application (new module) under the app, such as creating the admin module and index module under the app, which can be created by code or manually.
php think make:controller admin@index
php think make:controller index@index
After successful creation, there will be two folders, admin and index, under the app. At the same time, the index.php controller will be automatically created under the corresponding folder, and the code in the controller can be used directly.
4. Configure default applications
Modify config/app.php and modify the following configuration. If not, just add it directly.
// 设置默认应用名称
'default_app' => 'home',
// 开启自动多模式
'auto_multi_app' => true,
// 开启应用快速访问
'app_express' => false,
5. Modify pseudo-static
1. Apache is modified as follows
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [QSA,PT,L]
</IfModule>
2. nginx is modified as follows
location / {
try_files $uri $uri/ /index.php?$query_string;
index index.html index.htm index.php;
autoindex on;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
6. Configure environment variables
There is an ".example.env" environment variable example file in the project folder. This file is an example file, not a formally used file, so if you need to modify the environment variables, you can create a new file and name it ".evn" file, you can copy some configurations in the sample file to the new evn file
// 开启调试模式
APP_DEBUG = true
7. Visit items
At this point, you are done and you can directly enter the domain name to access the project.
8. Review the file structure
The file structure at this time is as follows. Two new folders are created in the app folder, index and admin, which are the two newly created modules. Like TP5, each module has controller files, model files, and view files. At the same time, under each module, there can also be related files under the current scope (function files, configuration files, etc.).
www WEB部署目录(或者子目录)
├─app 应用目录
│ ├─index 应用目录
│ │ ├─controller 控制器目录
│ │ │ ├─Index.php 控制器文件
│ │ ├─model 模型目录
│ │ ├─view 视图目录
│ │ ├─common.php 函数文件
│ │ ├─config 配置目录
│ │ ├─route 路由目录
│ │ └─ ... 更多类库目录
│ ├─admin 应用目录
│ │ ├─controller 控制器目录
│ │ ├─model 模型目录
│ │ ├─view 视图目录
│ ├─common.php 公共函数文件
│ └─event.php 事件定义文件
├─config 全局配置目录
│ ├─app.php 应用配置
│ ├─cache.php 缓存配置
│ ├─console.php 控制台配置
│ ├─cookie.php Cookie配置
│ ├─database.php 数据库配置
│ ├─filesystem.php 文件磁盘配置
│ ├─lang.php 多语言配置
│ ├─log.php 日志配置
│ ├─middleware.php 中间件配置
│ ├─route.php URL和路由配置
│ ├─session.php Session配置
│ ├─trace.php Trace配置
│ └─view.php 视图配置
├─public WEB目录(对外访问目录)
│ ├─index.php 入口文件
│ ├─router.php 快速测试文件
│ └─.htaccess 用于apache的重写
│
├─extend 扩展类库目录
├─runtime 应用的运行时目录(可写,可定制)
├─vendor Composer类库目录
├─.example.env 环境变量示例文件
├─composer.json composer 定义文件
├─LICENSE.txt 授权说明文件
├─README.md README 文件
├─think 命令行入口文件
├─.env 环境变量
For detailed description of the multi-application mode file structure, please refer to the official document https://www.kancloud.cn/manual/thinkphp6_0/1037483
Previous article: [ThinkPHP6 Series Learning-1] Download and deploy ThinkPHP6
Next article: [ThinkPHP6 Series Learning-3] Template Rendering