Home  >  Article  >  Backend Development  >  ThinkPHP Framework Guide in PHP

ThinkPHP Framework Guide in PHP

WBOY
WBOYOriginal
2023-05-21 08:51:051912browse

ThinkPHP is a well-known PHP open source framework. It is characterized by efficiency, simplicity, and ease of use, and can quickly build large-scale Web applications. This article will introduce you to the usage and precautions of the ThinkPHP framework.

1. Installation of ThinkPHP framework

1. Download ThinkPHP framework

You can download the ThinkPHP compressed package on the official website (http://www.thinkphp.cn/) and unzip. It can also be installed through Composer. No further details will be given here.

2. Create a new project

Create a new project on the web server and copy the decompressed ThinkPHP framework to the project directory.

2. Basic use of ThinkPHP framework

The core MVC (Model-View-Controller) architecture of ThinkPHP framework. Among them,

Model handles the persistence layer operation of data;
View is responsible for user interface presentation;
Controller serves as the business logic layer and is responsible for accepting and processing requests.

1. Create a controller

In the ThinkPHP framework, we can handle HTTP requests through the controller.

Create a new HomeController.php file and enter the following content:

class HomeController extends Controller
{
    public function index()
    {
        echo "Hello World!";
    }
}

2. Create a route

After completing the controller, we need to set up a route to access the controller. There are two ways to set routing: one is to set it in the configuration file, the other is to set it in the annotation (ThinkPHP 5 or above).

Set routing in the configuration file:

<?php
return [
    'route' => [
        'home/index' => 'HomeController/index',
    ]
];

The above code indicates that the request address /home/index will be routed to the index method of the HomeController controller.

Set routing in the annotation:

/**
 * @route('home/index')
 */
public function index()
{
    echo "Hello World!";
}

3. Access controller

After completing the above steps, we can call the one we just created by accessing /home/index controller.

3. Template engine of ThinkPHP framework

1. What is a template engine

The template engine separates data and business logic from the view file to facilitate later maintenance and Revise. The template engine built into the ThinkPHP framework is easy to use and powerful. By default, the controller will automatically look for a template file named "methodname.html" or "methodname.php".

2. Use the template engine

Steps to use the template engine in the controller:

①Embed the tag in the template file

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>模板示例</title>
    </head>
    <body>
        <h1>{$title}</h1>
        <ul>
            {foreach $list as $item}
                <li>{$item}</li>
            {/foreach}
        </ul>
    </body>
</html>

In the template file You can use tags such as Smarty and Blade. Here we use the tags of ThinkPHP's built-in template engine.

②Pass parameters in the controller

class HomeController extends Controller
{
    public function index()
    {
        $this->assign('title', 'Hello World');
        $this->assign('list', [
            'item1', 'item2', 'item3'
        ]);
        return $this->fetch();
    }
}

In this code, the assign() method sets the data into the template, and the fetch() method returns the rendered HTML.

The above is the use of ThinkPHP framework template engine.

4. Database operations of ThinkPHP framework

1. Database configuration

Modify the database configuration in the config/database.php file:

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

2. Query data

Use the Query class to operate, which can be achieved through the following methods:

<?php
namespace appindexcontroller;
use thinkDb;

class Index
{
    public function index()
    {
        //查询数据
        $list = Db::name('user')->order('id asc')->select();
        $this->assign('list', $list);
        return $this->fetch();
    }
}

The above code shows the method of querying user table (user) data.

3.Insert data

Use the insert() method of the Db class to insert data:

Db::name('user')->insert([
   'name' => '张三',
   'age' => 18,
   'email' => 'zhangsan@163.com'
]);

4.Update the data

Use the update() of the Db class ) method to update data:

Db::name('user')
    ->where('id', 1)
    ->update(['name' => '李四', 'age' => 20]);

5. Delete data

Use the delete() method of the Db class to delete data:

Db::name('user')
    ->where('id', 1)
    ->delete();

Summary

Through this article Introduction, I believe you already have a preliminary understanding of the use of the ThinkPHP framework. In actual project development, we need to learn more deeply about the various components of the framework, especially the use of models and routing. Hope this article can be helpful to you.

The above is the detailed content of ThinkPHP Framework Guide in PHP. 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