php产品添加模块实现代码,产品模块的代码

这是一个关于PHP后台管理系统的控制器类实现,包括产品管理的增删改查操作。`Product`类继承了`Common`类,使用了ThinkPHP的facade组件来处理HTTP请求和会话。类中定义了展示产品列表、添加产品、上传图片、编辑产品和删除产品的功能,确保了数据操作的统一返回格式。
摘要由CSDN通过智能技术生成

摘要:<?phpnamespace app\admin\controller;use app\admin\controller\Common;use app\admin\model\ProductModel;use app\admin\model\SortModel;use think\facade\Request;use think\facade\Session;class Product ex

namespace app\admin\controller;

use app\admin\controller\Common;

use app\admin\model\ProductModel;

use app\admin\model\SortModel;

use think\facade\Request;

use think\facade\Session;

class Product extends Common

{

public function index()

{

// 实例化模型

$product = new ProductModel();

// 查询数据并按照id进行排序每页八条数据

$products = $product->order('id', 'desc')->paginate(8);

// 将数据赋值给模板

$this->view->products = $products;

// 渲染产品列表

return $this->fetch();

}

public function add()

{

// 实例化并查询分类

$sorts = SortModel::all();

// 将数据赋值到模板

$this->view->sorts = $sorts;

//渲染产品添加界面

return $this->fetch();

}

public function upload()

{

// 获取上传图片信息

$file = Request::file('img');

// 验证图片信息并移动到指定目录

if ($info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload')) {

// 返回成功的提示信息

return json(['errno' => 0, 'data' => ['/upload/' . $info->getSaveName()]]);

} else {

// 返回失败的提示信息

return $file->getError();

}

}

public function DoAdd()

{

// 获取提交出来的数据

$data = Request::param();

// 将产品名独立出来

$title = $data['title'];

// 利用产品名作为查询条件查询对应的数据

$info = ProductModel::where('title', $title)->find();

// 判断是否查询到相同的产品名称

if ($info == true) {

// 返回提示信息

return ['res' => 0, 'msg' => '产品标题重复!'];

}

// 加入添加时间

$data['time'] = time();

// 添加发布管理员

$data['username'] = Session::get('username');

// 实例化模型

$product = new ProductModel();

// 进行添加并验证

if ($product->save($data)) {

// 返回提示信息

return ['res' => 1, 'msg' => '发布成功!'];

} else {

// 返回提示信息

return ['res' => 0, 'msg' => '发布失败!'];

}

}

public function edit()

{

$proId = Request::param('id');

$product = ProductModel::get($proId);

$this->view->product = $product;

// 渲染产品修改界面

return $this->fetch();

}

public function DoEdit()

{

// 获取提交过来的数据

$data = Request::param();

$product = new ProductModel();

$data['time'] = time();

$data['username'] = Session::get('username');

$info = $product->save([

'title' => $data['title'],

'desc' => $data['desc'],

'content' => $data['content'],

'once' => $data['once'],

'over_night' => $data['over_night'],

'time' => $data['time'],

'username' => $data['username'],

], ['id' => $data['id']]);

if ($info) {

return ['res' => 1, 'msg' => '更新成功!'];

} else {

return ['res' => 0, 'msg' => '更新失败!'];

}

}

public function del()

{

// 获取需要删除的产品id

$proId = Request::param('id');

$product = new ProductModel();

if ($product->destroy($proId)) {

return ['res'=>1,'msg'=>'删除成功!'];

}

}

}

批改老师:查无此人批改时间:2018-11-28 17:25:29

老师总结:写的不错,返回值也统一格式,少年继续加油。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值