hyperf使用之curd

续接上文:宝塔安装hyperf

上文说到如何在宝塔中安装hyperf框架,其余的运维面板也大差不差,就不再赘述了。

这里使用的工具的phpstorm

tips:如果你使用过laravel或者thinkphp5.1(6)的话,会发现其实大致操作是一致的,很容易就上手,最后还是要勤于看文档,有不懂的及时翻阅文档。

一、插件安装

启动hyperf成功访问后,还需要安装两个插件

PHP Annotation
Swoole IDE Helper

说一下作用:

PHP Annotation的作用是用来实现php中的注解定义,同时在hyperf中也有这么一小节讲述到路由注解的作用,详情移步:注解注解        注解

安装该插件后,即可使用

域名:9501/模块/控制器/方法  

 这样的方式去访问

例如这样:

如果不安装该路由注解,注定了只能在config/routes.php该路由文件中定义好短路由名称,再进行访问。

例如这样:

 

Swoole IDE Helper:用于编写swoole的时候提示代码的一个插件。

二、基本文件说明:

请根据该文件目录自行配置好mysql连接或其他信息,若配置env配置无效,请去config/autoload/找打databases.php配置mysql信息

三、代码展示

<?php

//严格类型检查(php7才引入)
declare(strict_types=1);

namespace App\Controller;
//引入模型admins
use App\Model\Admins;

//引入自动控制器类
use Hyperf\HttpServer\Annotation\AutoController;


/**
 * 通过 @AutoController() 注解定义路由
 * 文档地址:https://hyperf.wiki/2.2/#/zh-cn/quick-start/overview?id=%e9%80%9a%e8%bf%87-autocontroller-%e6%b3%a8%e8%a7%a3%e5%ae%9a%e4%b9%89%e8%b7%af%e7%94%b1
 * 一定要加注解路由,不然无法使用 域名/模块/控制器/方法 这样的方式去访问
 * 然后就会导致只能自己手动去config/routes.php(路由文件)自定义短路由访问
 */

/**
 * @AutoController()
 */
class HomeController extends AbstractController
{
    public function __construct(Admins $admins)
    {
        $this->admins = $admins;
        //$this->admins = new Admins();  //上面那段代码当然你也可以写成这样,不过这样就不是依赖注入了...
    }

    public function say()
    {
        $name = $this->request->input('name', 'anbin');
        $age = 18;
        $hobby = '睡觉';

        return [
            'age' => $age,
            'hobby' => $hobby,
            'message' => "Hello {$name}.",
        ];
    }

    /**
     * 增加
     * 浏览器访问:   http://ip地址:9501/home/add
     * 文档:https://hyperf.wiki/2.2/#/zh-cn/db/model?id=%e6%a3%80%e7%b4%a2%e5%8d%95%e4%b8%aa%e6%a8%a1%e5%9e%8b
     */
    public function add()
    {
        //模型插入数据
        /*
            $res = $this->admins->insert([
            'username' => 'anbin',
            'password' => '123456',
            'sex' => '1',
            'remarks' => 'anbin真帅'
        ]);
        */

        //模型插入数据后返回该条数据的id
        $res = $this->admins->insertGetId([
            'username' => 'anbin',
            'password' => '123456',
            'sex' => '1',
            'remarks' => 'anbin真帅'
        ]);


        return $this->rjson($res, '插入');

    }

    /**
     * 更新
     * 浏览器访问:   http://ip地址:9501/home/edit
     * 文档:  https://hyperf.wiki/2.2/#/zh-cn/db/model?id=%e6%9b%b4%e6%96%b0
     * @return array
     */
    public function edit()
    {
        $id = $this->request->input('id', 19);
        //update更新id为19的remarks属性数据
        /*
            $res = $this->admins
            ->where('id', $id)
            ->update(['remarks' => 'hyperf更新数据之update方法']);
        */

        //save更新id为19的remarks属性数据,需要先查出来才能更新

        $res = $this->admins->query()->find($id);
        $res->save(['remarks' => 'hyperf更新数据之save方法']);


        return $this->rjson($res, '更新');
    }

    /**
     * 查询
     * 浏览器访问:   http://ip地址:9501/home/select
     * 文档:https://hyperf.wiki/2.2/#/zh-cn/db/model?id=%e6%a3%80%e7%b4%a2%e5%8d%95%e4%b8%aa%e6%a8%a1%e5%9e%8b
     */
    public function select()
    {

        //$data =  $this->admins->query()->find(1);  //原生query方法查询id为1的数据

        //$data =  $this->admins->where('id', 1)->first(); //模型first方法查询id为1的数据

        //$data =  $this->admins->query()->find([1,8,10]); //原生query查询id为1,8,10的数据

        //$data =  $this->admins->all();  //模型all方法查询admins表中所有数据

        //$data =  $this->admins->get(); //模型get方法查询admins表中所有数据

        $data = $this->admins->paginate(2);  //模型自动分页,每页展示2条; 报错Class 'Hyperf\Paginator\Paginator' not fonund去这里https://hyperf.wiki/2.2/#/zh-cn/paginator?id=%e5%ae%89%e8%a3%85

        return $data;
    }

    /**
     * 删除
     * 浏览器访问:   http://ip地址:9501/home/del
     * 文档:  https://hyperf.wiki/2.2/#/zh-cn/db/model?id=%e5%88%a0%e9%99%a4%e6%a8%a1%e5%9e%8b
     * @return array
     */
    public function del()
    {
        //模型删除单条id数据,没传id则默认删除id为20的这条数据

        /*
            $id = $this->request->input('id', 20);
            $res = $this->admins->where('id', $id)->delete();
        */

        //通过主键除单条id数据,没传id则默认删除id为20的这条数据
        /*
            $id = $this->request->input('id', 20);
            $res = $this->admins->destroy($id);
        */

        //模型删除id为20,21的数据
        $res = $this->admins->whereIn('id', [20, 21])->delete();

        //通过主键删除id为20,21的数据
        //$res = $this->admins->destroy([20, 21]);

        return $this->rjson($res, '删除');
    }


    public function rjson($res, $msg)
    {
        if (empty($res)) {
            return ['code' => 450, 'msg' => $msg . '失败'];
        }
        return ['code' => 200, 'msg' => $msg . '成功'];
    }


}

模型代码,这里去除了抽象类:

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Model;


use Hyperf\DbConnection\Model\Model as BaseModel;

//引入软删除
use Hyperf\Database\Model\SoftDeletes;

class Admins extends BaseModel
{
    use SoftDeletes;
}

由于hyperf内部做了json处理,所以在return的时候返还正常的数组即可,不需要像tp或者laravel一样先json再return 

效果展示:

添加:

修改:

删除:

查询:

更多链式查询以及聚合函数、字段的自增与自减、更新json字段等等

请移步模型章节:模型

四、关闭debug

hyperf默认创建时间字段和更新时间字段是created_at 和updated_at ,这一点要注意,如果cli报错记得创建这两个字段;如果不想让hyperf接管这两字段,则需要在对于模型中设置public $timestamps = false;

 如下:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值