Function and definition of file entry



File entry definition

6.0 The default application entry file is located in public/index.php, by default The content is as follows:

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

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

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

If you have no special customization requirements, there is no need to make any changes to the entry file.

The entry file location is designed to make application deployment safer. Please try to follow the public directory as the only web-accessible directory. Other files can be placed in non-WEB-accessible directories.

Console entry file

In addition to the application entry file, the system also provides a console entry file, located in think in the project root directory (note that this file does not have any suffix).

The file code is as follows:

#!/usr/bin/env php
<?php
namespace think;

// 加载基础文件
require __DIR__ . '/vendor/autoload.php';

// 应用初始化
(new App())->console->run();

The console entry file is used to execute console commands, for example:

php think version

The system has built-in some commonly used console commands. If you When additional extensions are installed, corresponding console commands will also be added, all of which are executed through this entry file.

If there are class library directories such as controller, model, and view directly under the app directory, it is single application mode (default mode). If an application subdirectory is created under the app directory, it will automatically become multi-application mode. .

The directory structure differences between single application and multiple applications are as follows (mainly in the app directory).

Single application mode

├─app application directory

│ ├─controller     Controller directory

│ ├─model         Model directory

│ ├ ─ View View Catalog

│ └ ─ ... More types of library directory

# │

## ├ ─ PUBLIC Web directory (external access directory)

│ ├─index.php │ Entry file

│ ├─router.php Quick test file

│ └─.htaccess Rewrite for apache

├─config             Application configuration directory

├─route                  Route definition directory

├─runtime                                                                                                                                                                                                  outps out out outps out outps out field field field field field field field field field best to to be so so?

├─app application directory

│ ├─index                                                                                                                                                                                   

##’

│ │ v─VIEW View Catalog

# │ │ ├ ─CONFIG Configuration Catalog (Preferred)

│ │ └ ─ ... More types of library directory

│ ├─admin                Backend application

│ │ ├─controller Controller directory

│ │ ├─model Model directory

│ │ ├─view View directory

│ │ ├─config Configuration directory (Priority)

│ │ └ ─ ... More types of library directory

─ Public web directory (external access directory)

Use Rewrite of apache

├─config Application configuration directory

│ ├─index Index application configuration

│ └─admin Admin application configuration

# │

# ─ Route route definition directory

# ─ ─index index application route definition directory

# ─ a └ a └ └ └ ─Admin admin.

##│

├─runtime Runtime directory

│ ├─index             index application runtime directory

│ └─admin           admin application runtime directory

As can be seen from the directory structure, each application remains relatively independent, and each application has a corresponding entry file. The application can also maintain controller groups through multi-level controllers.

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. If your entry file name is inconsistent with the application, for example For your backend admin application, the entry file name uses think.php, then the entry file needs to be changed to:

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

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

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

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

Core class library directoryFramework core class library

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())->make('http');
$response = $http->path('path/to/app')->run();
$response->send();
$http->end($response);

Automatic Multi-application deployment

supports access to multiple applications in the same entry file, and supports application mapping and customization.

You only need to set

// 开启自动多应用模式
'auto_multi_app'    =>    true,

in the config/app.php configuration file, so you don’t have to create an entry file for each application but can automatically access multiple applications through the index.php entry file. If the URL access using the multi-entry method is:

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

After the automatic multi-application mode is turned on, it becomes

// 访问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. The latter is the routing of the application.

If you directly access

http://serverName/index.php

, you are actually accessing the index application (the default is the entry file name). If your default application is not index, you can specify the default application through the default_app configuration parameter .

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

Then visit

http://serverName/index.php

In fact, what you are accessing is the home application.

Application mapping

Application mapping only supports automatic multi-application mode

Supports application alias mapping, for example:

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

After applying mapping, the original application name will not be accessible. For example, the admin application above cannot be accessed directly, but can only be accessed through the think 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 subdomains or independent domain names To access, you can define the binding between the domain name and the application in the config/app.php configuration file.

Prohibit application access

Prohibit application access only supports automatic multi-application mode

If you do not want an application to be accessed through a URL, for example, you A common subdirectory is added to place some public class libraries, you can set

'deny_app_list' =>    ['common']



##

Directory locationDirectory descriptionGetting method
Root directory The directory where the project is located is automatically obtained by default and can be passed in when the entry file instantiates the App class. App::getRootPath()
Basic directory app directory under the root directory App::getBasePath()
Application directoryThe directory where the current application is located, if it is single application mode Same as the basic directory. If it is multi-application mode, it is app/application subdirectoryApp::getAppPath()
Configuration DirectoryconfigDirectory under the root directoryApp::getConfigPath()
Runtime directoryThe directory when the framework is runtime. Single application mode is the runtime directory of the root directory. Multi-application mode is runtime/application subdirectory#. ##App::getRuntimePath()
thinkDirectoryApp::getThinkPath()