tp6 tp5 框架 增删改查 脱坑必备

 tp6:注意点

1,想要添加视图文件 一定要先composer 引入 view    

2,返回视图的数据 查看是否需要转json格式     报变量类型错误 :数组    

3,Db 不能写成 DB   一定要引入门面类 

tp5:注意点

1,控制器名称必须大写 否则路由 定义找不到

 

php think make:controller   控制器名       创建资源控制器

php think make:model    模型层           创建模型层

控制器代码    

<?php
declare (strict_types = 1);

namespace app\controller;
use app\models\gaem;
use think\facade\Db;
use think\Request;

class GremController
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{

}

/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
return view('day15/index');
}

/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\response\Json
*/
public function save(Request $request)
{
//获取全部数据值
$data= $request->param();
//获取排除某些字段的值,即获取其他值
// $data = $request->except('loginToken');
//获取指定字段的值,即获取本次指定的值
// $data[]=$request->only(['g_name','g_type','g_bady','g_attr']);

//Db添加入库方法
Db::name('game')->save($data);
//模型层添加入库方法
// gaem::saves($data);

return json(['code'=>1,'msg'=>'添加成功','data'=>'']);

}

/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}

/**
* 显示编辑资源表单页.
*
* @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)
{
//
}

/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}

  模型层

<?php
namespace app\models;

use think\Model;

class gaem extends Model
{
    protected $table = "game";

    public static function saves($data)
    {
        return self::insert($data);
    }
}

  

路由   

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;

Route::get('think', function () {
    return 'hello,ThinkPHP6!';
});

Route::get('hello/:name', 'index/hello');


Route::resource('grem','GremController');

  

  

 

posted @ 2020-11-27 15:36  拉斯维加斯  阅读(503)  评论(0)    收藏  举报