Home  >  Article  >  PHP Framework  >  How to configure thinkphp5

How to configure thinkphp5

PHPz
PHPzOriginal
2023-04-17 09:49:591308browse

With the continuous development of Internet technology, PHP language has become a very popular web development language, and ThinkPHP is an excellent development framework in PHP. This article will introduce the configuration of ThinkPHP5 to help you better use this framework.

1. Environmental requirements

Before starting the configuration, we need to make some requirements for the environment. ThinkPHP5 requires PHP5.4 and above, and MySQL 5.0 and above. If your server meets these conditions, then we can start configuring the framework.

2. Directory structure and file usage

Let’s first take a look at the directory structure of ThinkPHP5:

ThinkPHP
├─app                 Application directory
│ ├─ Controller controller directory
│ ├ ─MODEL model directory
│ └ ─ View View Directory
├ ─CONFIG Configuration File Directory
— Public entry file and resource directory
─ ─ Static static static static static static static static static Resource catalog
│ #─.htaccess HTACCESS file
│ ├ ─ Favicon.ico website icon
│ └ ─index.php entrance file
─ Route route configuration directory
─ runtime running Time directory
├─thinkphp            ThinkPHP framework system directory
├─vendor               Composer class library directory
├─.htaccess                  -           htaccess file
├─composer.json                File
├─README.md Framework description file
└─think Command line entry file

The functions of each folder are as follows:

  1. app: Application directory, used to store controllers, models, view files.
  2. config: Configuration file directory, including database configuration, routing configuration, etc.
  3. public: Entry file and resource directory, including static resource directory, entry file and htaccess file.
  4. route: Routing configuration directory, configure routing rules.
  5. runtime: runtime directory, including cache files, log files, etc.
  6. thinkphp: ThinkPHP framework system directory, including core class libraries, function libraries, etc.
  7. vendor: Composer class library directory, including ThinkPHP extension class libraries, third-party class libraries, etc.
  8. .htaccess: htaccess file, used for URL rewriting.
  9. composer.json: Composer configuration file, used for package management.
  10. README.md: Framework description file.
  11. think: Command line entry file, used to execute various commands.

3. Database configuration

The database configuration file is in the database.php file in the \ThinkPHP\config directory. We need to configure the parameters in it according to the actual situation, such as the database host address. , database name, database user name and password, etc.

The specific configuration method is as follows:

return [
    // 数据库类型
    'type'            => 'mysql',
    // 服务器地址
    'hostname'        => 'localhost',
    // 数据库名
    'database'        => 'test',
    // 用户名
    'username'        => 'root',
    // 密码
    'password'        => '',
    // 端口
    'hostport'        => '',
    // 连接dsn
    'dsn'             => '',
    // 数据库连接参数
    'params'          => [],
    // 数据库编码默认采用utf8
    'charset'         => 'utf8',
    // 数据库表前缀
    'prefix'          => 'think_',
    // 数据库调试模式
    'debug'           => true,
    // 开启自动写入时间戳字段
    'auto_timestamp'  => true,
    // 开启字段缓存
    'fields_cache'    => true,
];

4. Routing configuration

The routing configuration file is in the \ThinkPHP\route directory. If you need to set the URL address variable, you need to modify it. The route.php file in the directory, the specific configuration method is as follows:

use think\Route;

Route::get('hello/:name', 'index/hello');

The above code means setting a get request route. When the user accesses http://your domain name/hello/abc, it will be mapped to The hello method of the index controller, and abc is passed in as a parameter of the method.

5. Application configuration

The application configuration file is in the app.php file in the \ThinkPHP\config directory. We can set the application configuration parameters by modifying the file, such as the default control devices, operating methods, etc.

The specific configuration method is as follows:

return [
    // 默认控制器名
    'default_controller'    => 'Index',
    // 默认操作名
    'default_action'        => 'index',
    // 异常处理handle类 留空使用 \think\exception\Handle
    'exception_handle'      => '',
];

6. Template configuration

The template configuration file is in the template.php file in the \ThinkPHP\config directory. We can pass this file To set the parameters of the template engine, such as setting the template cache path, tag start tag, tag end tag, etc.

The specific configuration method is as follows:

return [
    // 模板文件目录
    'view_path'    => '',
    // 模板后缀
    'view_suffix'  => 'html',
    // 模板引擎类型 支持 php think 支持扩展
    'type'         => 'Think',
    // 模板路径替换
    'view_depr'    => DIRECTORY_SEPARATOR,
    // 模板引擎普通标签开始标记
    'tpl_begin'    => '{',
    // 模板引擎普通标签结束标记
    'tpl_end'      => '}',
    // 标签库标签开始标记
    'taglib_begin' => '{',
    // 标签库标签结束标记
    'taglib_end'   => '}',
];

7. Summary

Through the above configuration method, we can flexibly configure the ThinkPHP5 framework, so that it can better meet the needs of our needs. At the same time, these configurations are also technologies that we often use in actual project development. Hope this article is helpful to everyone.

The above is the detailed content of How to configure thinkphp5. 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
Previous article:How to use thinkphp5Next article:How to use thinkphp5