Backend Development 3 min read

ThinkPHP6 Configuration and Environment Variable Setup Guide

This article explains how to work with the ThinkPHP6 config directory, rename and edit the .example.env file to .env, adjust database connection settings, and use the think\facade\Env facade to access environment variables in a PHP backend project.

php中文网 Courses
php中文网 Courses
php中文网 Courses
ThinkPHP6 Configuration and Environment Variable Setup Guide

1. Regular Configuration

The config folder is the default configuration directory for a ThinkPHP6 application. You can modify the existing configuration files or add custom ones, but avoid renaming files or changing unfamiliar settings, as this may render the application unusable.

2. env Environment Variable Definition

When you download ThinkPHP6, the project root contains a .example.env file. Rename this file to .env and edit its contents to define your environment variables.

<code>APP_DEBUG = true
[APP]
DEFAULT_TIMEZONE = Asia/Shanghai
[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = test
USERNAME = username
PASSWORD = password
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true
[LANG]
default_lang = zh-cn</code>

The APP_DEBUG = true line enables ThinkPHP6's debug mode, allowing you to see detailed error information during development.

To retrieve environment variables in code, import the think\facade\Env facade; variable names are case‑insensitive.

To configure the database, edit the .env file and set the following entries:

<code>DATABASE = tp
USERNAME = root
PASSWORD = root</code>

Correspondingly, update config/database.php to read these values using the env helper:

<code>// Database name
'database' => env('database.database', 'tp'),
// Username
'username' => env('database.username', 'root'),
// Password
'password' => env('database.password', 'root'),</code>

For the full tutorial and additional details, refer to the original article linked below.

Read the original article

backendDatabaseconfigurationPHPEnvironment VariablesThinkPHP6
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.