ThinkPHP is a PHP framework based on the MVC development model, used for the development of fast, scalable and easy-to-maintain web applications. In this article, we will learn how to use the power of the ThinkPHP framework to implement simple article editing functions in a web application.
We will create a module called "Article", which will contain the functions of creating, editing and deleting articles. We will start with the database and create a new data table "articles" which will store various properties of the articles such as title, content and status.
First, we need to create a database with a random name. Within this database, we will create a new table named "articles". This table will have the following columns:
id – This is a unique identifier for each article, it will be an integer, primary key and auto-incrementing.
title – This is the title of the article, it will be a string, up to 50 characters.
body – This is the main body of the article, it will be one large text.
status – This is the status of the article, it will be a boolean value.
created_at – This is the date timestamp when the article was created, it will be an integer.
updated_at – This is the date timestamp when the article was last updated, it will be an integer.
Next, in our project we will create a module called "Article", we can create a new module by using the following command in the terminal:
php think module Article
This will create a module called "Article" in our project. This module will contain the following controllers: Index, Create, Edit, Delete and Update. We will define the Articles table in the model of "Article" and implement the article list in the Index controller of "Article".
In our model, we need to use ThinkPHP ORM to define the Articles table. We can add the following code to the model file in order to define the Articles table:
namespace app\article\model;
use think\Model;
class Articles extends Model
{
// 数据表名 protected $table = 'articles'; // 主键名 protected $pk = 'id'; // 字段定义 protected $schema = [ 'id' => 'int', 'title' => 'string', 'body' => 'text', 'status' => 'boolean', 'created_at' => 'int', 'updated_at' => 'int', ];</p> <p>}</p> <p>Next, in our Index controller, we will use the ORM to get all the articles and pass them to displayed in the view. To achieve this we will use the following code: </p> <p><?php<br/>namespace app\article\controller;</p><p>use app\article\model\Articles;</p><p>class Index<br/>{</p><pre class="brush:php;toolbar:false">public function index() { // 获取所有文章 $articles = Articles::select(); // 渲染视图 return view('index', [ 'articles' => $articles, ]); }</p> <p>}</p> <p>In our view we will display the title and creation date of all articles and provide a link for users to edit and delete articles . The view file is as follows: </p> <p><!DOCTYPE html><br><html><br><head></p> <pre class="brush:php;toolbar:false"><title>文章列表</title>
Title | Creation Date | Action |
---|---|---|
title; ?> | created_at); ?> | $article->id]); ?>">Edit $article->id]); ?>">Delete |