Home > PHP Framework > ThinkPHP > body text

How to implement thinkphp article editing function

PHPz
Release: 2023-04-11 14:11:17
Original
658 people have browsed it

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(&#39;index&#39;, [
        &#39;articles&#39; => $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>
Copy after login


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(&#39;create&#39;);
}

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');
}</p>
<p>}</p>
<p>我们的Create控制器中有两个方法:index和create。index方法将渲染我们的表单视图,create方法将获取表单数据并在数据库中创建新的文章。</p>
<p>我们的表单视图将包含一个<form>标记,其中包含“标题”和“主体”输入字段,以及submit按钮。表单视图如下所示:</p>
<p><!DOCTYPE html><br><html><br><head></p>
<pre class="brush:php;toolbar:false"><title>创建文章</title>
Copy after login


创建文章

<label for="title">标题</label>
<input type="text" name="title" id="title">

<label for="body">主体</label>
<textarea name="body" id="body"></textarea>

<button type="submit">创建</button>
Copy after login


在我们的“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');
}</p>
<p>}</p>
<p>我们的Edit控制器中也有两个方法:index和update。index方法将获取当前文章的数据,并渲染我们的表单视图。update方法将获取表单数据并更新文章。</p>
<p>我们的表单视图将包含一个<form>标记,其中包含输入字段,以供用户编辑当前文章的标题和主体。表单视图显示如下:</p>
<p><!DOCTYPE html><br><html><br><head></p>
<pre class="brush:php;toolbar:false"><title>编辑文章</title>
Copy after login


编辑文章

<input type="hidden" name="id" value="<?php echo $article->id; ?>">

<label for="title">标题</label>
<input type="text" name="title" id="title" value="<?php echo $article->title; ?>">

<label for="body">主体</label>
<textarea name="body" id="body"><?php echo $article->body; ?></textarea>

<button type="submit">更新</button>
Copy after login


在我们的“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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template