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', ];
}
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:
namespace app\article\controller;
use app\article\model\Articles;
class Index
{
public function index() { // 获取所有文章 $articles = Articles::select(); // 渲染视图 return view('index', [ 'articles' => $articles, ]); }
}
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:
<title>文章列表</title>
Article list
Title | Creation Date | Action |
---|---|---|
title; ?> | created_at); ?> | $article->id]); ?>">Edit $article->id]); ?>">Delete |
在我们的“Article”的Create控制器中,我们将显示一个表单,以供用户创建新的文章。表单将包含标题和主体字段,以及submit按钮。我们将使用以下代码来实现:
namespace app\article\controller;
use app\article\model\Articles;
use think\Request;
class Create
{
public function index() { // 渲染视图 return view('create'); } public function create(Request $request) { // 获取表单数据 $title = $request->param('title'); $body = $request->param('body'); // 创建新文章 $article = new Articles(); $article->title = $title; $article->body = $body; $article->status = true; $article->created_at = time(); $article->updated_at = time(); $article->save(); // 跳转到文章列表页面 return redirect('/article/index'); }
}
我们的Create控制器中有两个方法:index和create。index方法将渲染我们的表单视图,create方法将获取表单数据并在数据库中创建新的文章。
我们的表单视图将包含一个
在我们的“Article”的Edit控制器中,我们将显示与Create视图相似的表单,但是表单将包含当前文章的标题和主体字段。我们将使用以下代码实现:
namespace app\article\controller;
use app\article\model\Articles;
use think\Request;
class Edit
{
public function index(Request $request) { // 获取文章ID $id = $request->param('id'); // 获取文章 $article = Articles::find($id); // 渲染视图 return view('edit', [ 'article' => $article, ]); } public function update(Request $request) { // 获取表单数据 $id = $request->param('id'); $title = $request->param('title'); $body = $request->param('body'); // 更新文章 $article = Articles::find($id); $article->title = $title; $article->body = $body; $article->updated_at = time(); $article->save(); // 跳转到文章列表页面 return redirect('/article/index'); }
}
我们的Edit控制器中也有两个方法:index和update。index方法将获取当前文章的数据,并渲染我们的表单视图。update方法将获取表单数据并更新文章。
我们的表单视图将包含一个
在我们的“Article”的Delete控制器中,我们将删除当前文章。我们将使用以下代码实现:
namespace app\article\controller;
use app\article\model\Articles;
use think\Request;
class Delete
{
public function index(Request $request) { // 获取文章ID $id = $request->param('id'); // 删除文章 Articles::destroy($id); // 跳转到文章列表页面 return redirect('/article/index'); }
}
我们的Delete控制器中只有一个方法:index。这个方法将获取当前文章的ID,并从数据库中删除它。然后,它将重定向到文章列表页面。
现在我们已经完成了我们的“Article”模块。我们可以在我们的应用程序中使用以下URL访问它:
/article/index – 文章列表
/article/create – 创建文章
/article/edit/id – 编辑文章
/article/delete/id – 删除文章
我们已经成功地使用ThinkPHP框架创建了一个简单的文章编辑应用程序。现在,我们可以使用这些知识来创建更复杂的Web应用程序。
The above is the detailed content of How to implement thinkphp article editing function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor