Create and Run a ThinkPHP 8 Project
What is ThinkPHP?
ThinkPHP is an open-source PHP web framework created by Chinese PHP developers. It aims to provide developers with a simple and efficient toolkit to build modern web applications. ThinkPHP offers a rich set of features, such as routing, session management, caching, and authentication, simplifying common web development tasks.
Key Features and Advantages of ThinkPHP
- Simplicity and Efficiency: ThinkPHP's syntax is concise and efficient, enhancing code readability and maintainability.
- Rich Feature Set: Including routing, authentication, session management, and caching, significantly simplifying development work.
- Powerful ORM: ThinkPHP provides sophisticated database operation capabilities, making database operations easier.
- Modular Design: Easily integrate third-party libraries and extensions using the Composer package manager.
- Strong Community Support: A large developer community with a rich ecosystem.
ThinkPHP can help developers quickly build high-quality web applications and APIs, suitable for projects of all sizes, from small applications to large enterprise-level systems.
Create and Run a ThinkPHP 8 Project Using ServBay
In this article, we will create and run a ThinkPHP 8 project using the PHP environment provided by ServBay. We will use ServBay's 'Site' feature to set up a web server and achieve project access through simple configurations.
Note: If you have been a user of NGINX or Apache
ServBay uses Caddy as the web server by default. For users migrating from NGINX and Apache to ServBay, there are some key changes to be aware of:
Caddy Configuration
ServBay has built-in Caddy, and the default configuration is optimized and debugged. Developers only need to manage the site through ServBay's 'Site' feature without manually modifying the Caddy configuration file.
Rewrite Rules and .htaccess
In NGINX and Apache, developers usually need to write their own rewrite rules and .htaccess files to handle URL rewriting and other configurations. However, ServBay has already configured Caddy's rules out of the box, so developers do not need to write these rules unless there are special requirements.
Learn More
For more related information, please refer to Rewrite and htaccess, How to Migrate Apache Site to ServBay, How to Migrate NGINX Site to ServBay.
Create a ThinkPHP Project
TIP
ServBay recommends placing your website in the /Applications/ServBay/www
directory for easy management.
Install Composer
ServBay comes with Composer installed, so there’s no need to install it separately.
Create a ThinkPHP Project
Use Composer to create a new ThinkPHP project:
bashcd /Applications/ServBay/www composer create-project topthink/think servbay-thinkphp-app
1
2Enter the Project Directory
Enter the newly created ThinkPHP project directory:
bashcd /Applications/ServBay/www/servbay-thinkphp-app
1
Initialization Configuration
Configure Environment Variables
In the
config/database.php
file, configure the database connection information and other environment variables. Ensure the following configurations are correctly set:php'type' => 'mysql', 'hostname' => '127.0.0.1', 'database' => 'servbay_thinkphp_app', 'username' => 'root', 'password' => 'password', 'hostport' => '3306',
1
2
3
4
5
6
Configure the Web Server
DANGER
ThinkPHP's rewrite rules are slightly different from general rules, so customization is necessary.
Using ServBay's 'Site' feature, access the ThinkPHP project through the web server. In ServBay's 'Site' settings, add a new site:
- Name:
My First ThinkPHP Dev Site
- Domain:
servbay-thinkphp-test.local
Then click Custom Configuration
at the top right, and enter the following content below
encode zstd gzip
import set-log servbay-thinkphp-test.local
tls internal
@canonicalPath {
file {
try_files {path}/index.php
}
not path */
}
redir @canonicalPath {path}/ 308
root * /Applications/ServBay/www/servbay-thinkphp-app/public
route {
try_files {path} {path}/ /index.php?s={path}&{query}
php_fastcgi unix//Applications/ServBay/tmp/php-cgi-8.3.sock
}
file_server
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
TIP
If different PHP versions are needed, change php-cgi-8.3.sock
to the corresponding PHP version. Also, remember to change set-log
to the website's corresponding domain name.
For detailed setup steps, please refer to Add the First Website.
Add Sample Code
Add the following code to the route/app.php
file to output "Hello ServBay!":
Route::get('servbay', function () {
return 'Hello ServBay!';
});
2
3
Access the Website
Open your browser and visit https://servbay-thinkphp-test.local/servbay
, and you should see the webpage output Hello ServBay!
.
NoSQL Database Example
Memcached Example
Install Memcached Extension
In ServBay, the Memcached extension is already preinstalled, so no additional installation is required.
Configure Memcached
Configure Memcached connection information in the
config/cache.php
file:php'type' => 'memcached', 'host' => '127.0.0.1',
1
2Using Memcached
Use cache in the controller:
phpuse think\facade\Cache; Route::get('/memcached', function () { Cache::set('key', 'value', 600); return Cache::get('key'); });
1
2
3
4
5
6
Redis Example
Install Redis Extension
In ServBay, the Redis extension is already preinstalled, so no additional installation is required.
Configure Redis
Configure Redis connection information in the
config/cache.php
file:php'type' => 'redis', 'host' => '127.0.0.1', 'port' => 6379, 'password' => '',
1
2
3
4Using Redis
Use cache in the controller:
phpuse think\facade\Cache; Route::get('/redis', function () { Cache::set('key', 'value'); return Cache::get('key'); });
1
2
3
4
5
6
Relational Database Example
Create Database Structure and Migration Files
Install Database Migration Tool
To use ThinkPHP's database migration tool, first install it:
bashcomposer require topthink/think-migration
1Create Migration File
Use ThinkPHP's command-line tool to create a migration file:
bashphp think migrate:create CreateUserTable
1Edit Migration File
Find the newly created migration file in the
database/migrations
directory and edit it to define the database table structure:phppublic function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); }
1
2
3
4
5
6
7
8
9Run Migration
Use ThinkPHP's command-line tool to run the migration and create the database table:
bashphp think migrate:run
1
MySQL Example
Configure MySQL
Configure MySQL connection information in the
config/database.php
file:php'type' => 'mysql', 'hostname' => '127.0.0.1', 'database' => 'servbay_thinkphp_app', 'username' => 'root', 'password' => 'password', 'hostport' => '3306',
1
2
3
4
5
6Insert User Data
Insert user data in the controller:
phpuse think\Db; Route::get('/mysql-add', function () { Db::table('users')->insert([ 'name' => 'ServBay', 'email' => '[email protected]', ]); return 'User added'; });
1
2
3
4
5
6
7
8
9Using MySQL
Call the database in the controller:
phpuse think\Db; Route::get('/mysql', function () { $users = Db::table('users')->select(); return json($users); });
1
2
3
4
5
6
PostgreSQL Example
Configure PostgreSQL
Configure PostgreSQL connection information in the
config/database.php
file:php'type' => 'pgsql', 'hostname' => '127.0.0.1', 'database' => 'servbay_thinkphp_app', 'username' => 'root', 'password' => 'password', 'hostport' => '5432',
1
2
3
4
5
6Insert User Data
Insert user data in the controller:
phpuse think\Db; Route::get('/pgsql-add', function () { Db::table('users')->insert([ 'name' => 'ServBay', 'email' => '[email protected]', ]); return 'User added'; });
1
2
3
4
5
6
7
8
9Using PostgreSQL
Call the database in the controller:
phpuse think\Db; Route::get('/pgsql', function () { $users = Db::table('users')->select(); return json($users); });
1
2
3
4
5
6
By following the above steps, you successfully created and ran a ThinkPHP 8 project, managed and accessed your project using the features provided by ServBay, and connected to multiple databases and fetched data.