ThinkPHP5.1资源控制器Blog简单案例之控制器

ThinkPHP5.* / 577人浏览 / 0人评论

ThinkPHP5.1资源控制器Blog简单案例,使用路由,开启了强制路由限制,在postman上使用了接口调用,也是参考了官网的简单案例,实现了增删改查,再简单的必须熟练,效率才能提上去。

简介

ThinkPHP5.1资源控制器Blog简单案例,使用路由,开启了强制路由限制,在postman上使用了接口调用,也是参考了官网的简单案例,实现了增删改查,再简单的必须熟练,效率才能提上去。

控制器代码

<?php
/*
 * @Author: your name
 * @Date: 2021-04-10 11:15:10
 * @LastEditTime: 2021-04-21 17:44:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \thinkphp5.1.4\application\api\controller\Blog.php
 */

namespace app\api\controller;

use think\Controller;
use think\Request;
use app\api\services\Blogs;

class Blog extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $result = (new Blogs())->lists();
        $this->result($result, 200, '博文列表', 'json',);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->post();
        $result = Blogs::create($data);
        $this->result($result, 200, '新建博文成功', 'json',);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $result = Blogs::read($id);
        $this->result($result, 200, '博文详情', 'json',);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->post();
        $result = Blogs::save($id, $data);
        $this->result($result, 200, '更新博文成功', 'json',);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Blogs::delete($id);
        $this->result($result, 200, '博文删除', 'json',);
    }
}

ThinkPHP5.1资源控制器Blog简单案例之postman:

https://www.fxzbcn.com/html/thinkphp5/thinkphp51ziyuankongzhiqiBlogjiandananlizhipostman.html

ThinkPHP5.1资源控制器Blog简单案例之控制器:

https://www.fxzbcn.com/html/thinkphp5/thinkphp51ziyuankongzhiqiBlogjiandananlizhikongzhiqi.html

ThinkPHP5.1资源控制器Blog简单案例之服务层方法:

https://www.fxzbcn.com/html/thinkphp5/thinkphp51ziyuankongzhiqiBlogjiandananlizhifuwucengfangfa.html

ThinkPHP5.1资源控制器Blog简单案例之模型方法:

https://www.fxzbcn.com/html/thinkphp5/thinkphp51ziyuankongzhiqiBlogjiandananlizhimoxingfangfa.html

ThinkPHP5.1资源控制器Blog简单案例之数据表结构:

https://www.fxzbcn.com/html/thinkphp5/thinkphp51ziyuankongzhiqiBlogjiandananlizhishujubiaojiegou.html

转载注明:

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我