之前,我们一键生成了增删改查页面(参考博客:快速一键生成增删改查页面(附菜单栏添加&翻译详细解读)--Dcat-Admin框架实战(二) https://blog.csdn.net/have_a_cat/article/details/115623669),为了后续开发得心应手,我们探究下增删查改是怎么实现的。
目录
一、控制器代码分析
1、前情介绍
2、分析ContractRecordController.php
二、代码布局分析
2、分析AdminController.php
总结
●以合同记录控制器为例子讲解,文件位于/home/admin/test10/app/Admin/Controllers/ ContractRecordController.php
●合同记录控制器ContractRecordController在快速一键增删改查(附菜单栏添加&翻译详细解读)--Dcat-Admin框架实战(二)中生成。
(1)ContractRecordController共分为三部分:①数据表格,grid()函数;②数据详情,detail($id)函数;③数据表单。
(2)上面三个部分中,每个部分定义页面显示的内容。比如看ContractRecordController.php中的grid(),以及代码与页面显示的对应关系。
(AdminController.php 位于/home/admin/test10/vendor/dcat/laravel-admin/src/Controllers/目录下)
关键页面和关键接口,都已用中文注释于代码中。(更多博文,欢迎来我的博客学习交流https://blog.csdn.net/have_a_cat)
namespace Dcat\Admin\Controllers;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Routing\Controller;
class AdminController extends Controller
{
/**
* Title for current resource.
*
* @var string
*/
/* 页面标题 */
protected $title;
/**
* Set description for following 4 action pages.
*
* @var array
*/
/* 页面描述 */
protected $description = [
// 'index' => 'Index',
// 'show' => 'Show',
// 'edit' => 'Edit',
// 'create' => 'Create',
];
/**
* Get content title.
*
* @return string
*/
/* title()函数: 返回页面标题 */
protected function title()
{
return $this->title ?: admin_trans_label();
}
/**
* Get description for following 4 action pages.
*
* @return array
*/
/* description()函数: 返回页面描述*/
protected function description()
{
return $this->description;
}
/**
* Index interface.
*
* @param Content $content
*
* @return Content
*/
/* 列表页 */
public function index(Content $content)
{
if (request(Grid::IFRAME_QUERY_NAME)) {
return $content->full()->body($this->iFrameGrid());
}
return $content
->title($this->title())
->description($this->description()['index'] ?? trans('admin.list'))
->body($this->grid());
}
/**
* Show interface.
*
* @param mixed $id
* @param Content $content
*
* @return Content
*/
/* 详情页 (更多博文,欢迎来我的博客学习交流https://blog.csdn.net/have_a_cat) */
public function show($id, Content $content)
{
return $content
->title($this->title())
->description($this->description()['show'] ?? trans('admin.show'))
->body($this->detail($id));
}
/**
* Edit interface.
*
* @param mixed $id
* @param Content $content
*(更多博文,欢迎来我的博客学习交流https://blog.csdn.net/have_a_cat)
* @return Content
*/
/* 编辑页 */
public function edit($id, Content $content)
{
return $content
->title($this->title())
->description($this->description()['edit'] ?? trans('admin.edit'))
->body($this->form()->edit($id));
}
/**
* Create interface.
*
* @param Content $content
*
* @return Content
*/
/* 新增页 */
public function create(Content $content)
{
return $content
->title($this->title())
->description($this->description()['create'] ?? trans('admin.create'))
->body($this->form());
}
/**
* Update the specified resource in storage.
*(更多博文,欢迎来我的博客学习交流https://blog.csdn.net/have_a_cat)
* @param int $id
*
* @return \Illuminate\Http\Response
*/
/* 更新接口 */
public function update($id)
{
return $this->form()->update($id);
}
/**
* Store a newly created resource in storage.
*
* @return mixed
*/
/* 新增接口 */
public function store()
{
return $this->form()->store();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
/* 删除/批量删除接口 */
public function destroy($id)
{
return $this->form()->destroy($id);
}
}
增--create(Content $content)-- 新增页
----store()--新增接口
删--destroy($id)-- 删除/批量删除接口
查--index(Content $content)--列表页
----show($id, Content $content)-- 详情页
改--edit($id, Content $content)-- 编辑页
----update($id)-- 更新接口