Home >Backend Development >PHP Tutorial >How to use ThinkPHP framework in PHP programming?

How to use ThinkPHP framework in PHP programming?

WBOY
WBOYOriginal
2023-06-12 09:24:101237browse

As a widely used server-side programming language, PHP has been widely used in the field of Internet development and has been recognized by many developers. In the field of PHP development, using frameworks has become a very popular way. As a high-performance, highly scalable PHP development framework, ThinkPHP has a good reputation among PHP frameworks. In this article, we will take a deep dive into how to use the ThinkPHP framework in PHP programming.

1. Create a ThinkPHP project

First, we need to install the PHP environment in the local server. Here we recommend using XAMPP as our local PHP environment. After the download and installation is completed, we create a new project folder in the root directory of the website and extract the ThinkPHP framework into this folder.

Next, we enter the project folder, find the index.php file in the public directory and open it. Around line 9 we can find the following code:

//定义应用目录
define('APP_PATH', __DIR__ . '/../application/');

By modifying this line of code, we can specify the application directory as the current directory:

//定义应用目录
define('APP_PATH', __DIR__ . '/');

After saving the file, we open the browser, Enter http://localhost/ThinkPHP/ and you can see the welcome interface of ThinkPHP!

2. Basic Application

Next, we will learn how to create a basic application in ThinkPHP. In the application directory, we create a module named Index, which can be easily implemented through the CLI tool provided by the framework. The command is as follows:

php think build --module Index

Next, create a controller named Index under the Index module, And some basic methods:

namespace appindexcontroller;  //命名空间

class Index  //类名
{
    public function index()  //默认访问方法
    {
        return 'Hello ThinkPHP';
    }

    public function hello($name = 'ThinkPHP')  //传递参数
    {
        return 'Hello ' . $name;
    }
}

Next, we need to configure this controller and methods in routing. In the application's route directory, we can achieve this through the following code:

use thinkRoute;

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

Among them, the first line of code specifies the default access path, and the second line of code specifies a method for passing parameters.

Finally, enter http://localhost/ in the browser, and we can see the page we just created.

3. Template output

In ThinkPHP, we can use template output to build HTML pages. In this article, we will use ThinkPHP’s built-in template engine for demonstration.

First, create a new directory named view under the Index module, and create a file named index.html in this directory. In the file, we can use the syntax provided by the framework to complete the construction of the HTML page. The code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{site_title}</title>
</head>
<body>
    <h1>{title}</h1>
    <p>{content}</p>
</body>
</html>

In the controller, we bind data to the template through the following code:

public function template()
{
    $data = [
        'title' => 'ThinkPHP',
        'content' => 'Welcome to ThinkPHP',
        'site_title' => 'ThinkPHP Application'
    ];
    return $this->fetch('index', $data);
}

In the code, the fetch method outputs the template to the browser and binds the incoming data to the fields in the template file.

Finally, add the following code to the routing rules:

Route::get('/template', 'index/Index/template');

Enter http://localhost/template in the browser, and we can see the page output using the template!

4. Database Operation

In practical applications, we often need to operate the database to store and obtain data. In ThinkPHP, we can easily complete these operations. Let's demonstrate how to use ThinkPHP to add, delete, modify and query the database.

First, we need to configure the database connection information in the application. In the config directory, we can find the database.php file and edit it. The following is a simple example for connecting to a database named test and logging in as the root user:

return [
    // 数据库类型
    'type'        => 'mysql',
    // 服务器地址
    'hostname'    => 'localhost',
    // 数据库名
    'database'    => 'test',
    // 数据库用户名
    'username'    => 'root',
    // 数据库密码
    'password'    => '',
    // 数据库编码默认采用utf8
    'charset'     => 'utf8',
];

After the database configuration is completed, we can add operations to the database in the controller . Taking the query operation as an example, we can use the following code to implement it:

use thinkDb;

class Index
{
    public function select()
    {
        //查询操作
        $result = Db::table('think_user')->where('status', '1')->select();
        var_dump($result);
    }
}

In the code, the Db class provides us with a simple and convenient way to perform operations such as addition, deletion, modification, and query. Here, we call the select method and set the query condition to status=1.

Finally, we need to add the following code to the routing rules to achieve access:

Route::get('/select', 'index/Index/select');

Enter http://localhost/select in the browser, and we can see the query results!

Summary

This article explores in depth how to use the ThinkPHP framework in PHP programming, including knowledge points in creating projects, basic applications, template output, and database operations. I believe that through studying this article, you will have a deeper understanding of the ThinkPHP framework, and you can also use the framework more conveniently in PHP development.

The above is the detailed content of How to use ThinkPHP framework in PHP programming?. 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