ThinkPHP is an enterprise-level development framework based on PHP. It has the characteristics of fast development, efficiency, security and stability, and is deeply loved and favored by the majority of enterprises and developers. In the process of developing using ThinkPHP, the setting and management of the directory structure is a very critical step. In this article, we will take a deep dive into how to do good directory management in ThinkPHP.
1. ThinkPHP directory structure
Before understanding how to manage the directory, let’s first understand the directory structure of ThinkPHP. The directory structure of ThinkPHP is divided into three parts, namely the application directory, the root directory and the extension directory.
Application directory (Application): The application directory refers to the directory we use to store application-related files, such as controllers, models, views, etc.
Root directory (Public): The root directory refers to the directory where entry files and other files and resources related to project operation are stored, such as index.php files and some pictures.
Extension directory (ThinkPHP): The extension directory stores the core code of ThinkPHP, including controllers, models, configuration files, etc.
2. ThinkPHP directory structure settings
1. Change the project root directory
In the directory structure of ThinkPHP, the file in the project root directory stores the entry file, such as index.php wait. In order to better manage the directory, we can change the project root directory from the default Public directory to other custom folders.
First, we need to set it in the project’s configuration file config.php.
$config = [
// 将项目根目录更改为 mypublic 'app' => [ 'root_namespace' => [ 'app' => 'application', 'mypublic' => 'public', ], 'root_path' => [ 'application' => __DIR__ . '/../application/', 'public' => __DIR__ . '/../mypublic/', ], ],
];
In the above code, we change the project root directory from the default Public directory to the mypublic directory. It should be noted that we also need to make modifications in the project’s entry file index.php. The modification method is as follows:
// Add the original require to the path of the customized mypublic folder
/ /require DIR . '/../public/index.php';
require DIR . '/../mypublic/index.php';
2. Add a new application directory
In the process of project development, sometimes it is necessary to add multiple application directories to a project. For example, if we need to provide different operating interfaces for different users, we need to specify a dedicated directory for different applications. At this time, we need to add a new application directory under the application directory of the project and perform relevant configurations.
Copy the application
directory to a new directory myApplication1
, and add an application directory configuration in config.php
:
$config = [
'app' => [ 'pathinfo_depr'=>'-', 'url_html_suffix'=>'html', // 等价于配置文件中的 'app_namespace' => 'app', 'app_namespace'=>'app', 'auto_bind_module' => true, 'root_namespace' => [ 'app' => 'application', 'public' => 'public', 'myApplication1' => 'application1', //新增一个应用目录 myApplication1 ], 'root_path' => [ 'application' => __DIR__ . '/../application/', 'public' => __DIR__ . '/../public/', 'myApplication1' => __DIR__ . '/../application1/', //新增的 myApplication1 目录 ], ],
];
It should be noted that adding a new application directory may have an impact on the operation of the framework. You need to pay attention to ensuring the robustness of the code during operation. sex.
3. ThinkPHP directory management
When using ThinkPHP for development, directory management is a very critical step, which can greatly improve the efficiency of development and the readability of the code. During the project development process, it is necessary to set up and manage each directory according to the actual situation.
1. Adjust the application directory structure
We can divide the application directory, and each application directory is separated according to the actual business for better development and management.
For example, for an e-commerce website, it can be divided into three application directories: frontend, backend and mobile phone, each with its own controller, view and other files. In this way, codes between different applications will not be mixed together, and it will also facilitate code debugging, management and maintenance.
2. Divide the core code
Normally, our code involves a lot of business logic. In order to make the code clearer and easier to understand, we can divide the code according to functions. For example, classify and manage controller, model, view, configuration and other files according to different folders. In this way, even when the amount of code is large, we can quickly find the code we need.
3. Unified naming convention
When performing directory management, we also need to follow a unified naming convention to facilitate reading and management. For example, controller files can be named according to their functions, such as UserController, GoodsController, etc. For model files, you can use the same naming method as the controller, such as UserModel, GoodsModel, etc.
At the same time, when defining variables, functions and classes, you also need to pay attention to following unified naming conventions to facilitate cross-file calling and use.
4. Summary
Directory management is an important task in the software development process. It can improve the readability, maintainability and scalability of the project code. When using ThinkPHP for development, we need to set up the directory structure according to the actual situation, and at the same time carry out unified naming conventions and code management to facilitate better development and management of projects.
The above is the detailed content of How to make a directory in thinkphp. For more information, please follow other related articles on the PHP Chinese website!