Home  >  Article  >  Backend Development  >  How to automatically generate modules and directories in Thinkphp5.0

How to automatically generate modules and directories in Thinkphp5.0

不言
不言Original
2018-06-07 15:31:262365browse

This article mainly introduces the method of automatically generating modules and directories in Thinkphp5.0, and briefly analyzes the structure, directory, creation and operation methods of Thinkphp5.0. Friends in need can refer to the examples of this article

Describes how Thinkphp5.0 automatically generates modules and directories. I share it with you for your reference. The details are as follows:

Thinkphp5.0 has been released for some time. It is said that the performance has been greatly improved. According to the official words, ThinkPHP5.0 version is a subversive and reconstructed version. , adopts a new architectural idea, introduces many new PHP features, optimizes the core, reduces dependencies, achieves true lazy loading, and makes a lot of optimizations for API development. It’s time to download a copy and study it. Today we mainly talk about its automatic creation of modules and directories.

Thinkphp5.0 automatically generates modules compared to ThinkPHP3.2. There are indeed big changes.

Once again, we mainly discuss the Thinkphp5.0 generation module.

Preparation work

First download ThinkPHP5.0 from the official website. After downloading, the file structure is as follows:

Only keep the thinkphp directory and delete all other directories and files (you can move them to other places first for subsequent use)

Start generating the project directory now

Creating a directory seems simple with thinkphp3.2, but thinkphp5.0 is more flexible. I can't help but think that good people make complex things simple; mediocrity makes simple things complicated; fools make simple things simpler and complex things more complicated.

1. Create the entry file index.php. Although the official emphasizes that the location of the entry file can be placed at will, but for the convenience of learning, it is still placed in the root directory

// 应用入口文件
define('APP_PATH', 'application/'); // 定义项目路径,和之前3.2版本没有区别
define('APP_AUTO_BUILD',true); //开启自动生成
define('APP_DEBUG', true);// 开启调试模式
define('MODULE','module');
// 加载框架引导文件
require 'thinkphp/start.php';
// 执行应用
\think\App::run();

In fact, I personally think that TP Why didn't the official place the execution of the program\think\App::run(); in the start.php directory? Finally, it was discovered that TP5.0 introduced a define('APP_AUTO_RUN', true); constant. If the constants are defined on the index.php page, the project will be executed automatically. There is no need to add \think\App::run();

2. Create the project name and build.php file

Personally, I feel that one thing that thinkphp5.0 does poorly is that the project folder defined above needs to be created by yourself. The reason is that the project creation configuration file build.php must be placed in it. But why doesn’t TP official place build.php in the root directory? Then automatically generate the application folder? At this point, I think TP3.2 is better than 5.0.

Now let’s discuss build.php. The official default bulid.php content is as follows. Now let’s discuss it.

return [
  // 生成运行时目录
  '__dir__' => ['runtime/cache', 'runtime/log', 'runtime/temp', 'runtime/template'],
  '__file__' => ['common.php'],
  // 定义index模块的自动生成
  'index'  => [
    '__file__'  => ['common.php'],
    '__dir__'  => ['behavior', 'controller', 'model', 'view'],
    'controller' => ['Index', 'Test', 'UserType'],
    'model'   => [],
    'view'    => ['index/index'],
  ],
  // 。。。 其他更多的模块定义
];

You can define files and directories that need to be automatically generated for each module, as well as MVC kind.

__dir__ represents the generated directory (supports multi-level directories)
__file__ represents the generated file (the config.php file will be generated by default if not defined)
controller represents the generated controller class
model represents Generate model class
view means generating html files (supports subdirectories)

Automatically generate APP_PATH as the starting directory, __dir__ and __file__ mean that directories and files need to be automatically created, and others mean Automatically generated for modules.

The automatic generation of modules takes APP_PATH.'Module name/' as the starting directory.

Finally, run the index.php file directly.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

ThinkPHP implements cross-module calling

ThinkPHP template replacement and system constants and application examples

The above is the detailed content of How to automatically generate modules and directories in Thinkphp5.0. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn