一,修改系统自带的表和页面
- 环境:laravel9+dcat2+php8.2
此处我想修改admin_users表,添加一个park_id 关联到parks表,须要修改管理员页面
使用继承重写的思路,最好不要直接去修改vendor包里面的dcat源码,只需要找到相应位置,然后复制过来,重写即可。
新建一个model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Dcat\Admin\Models\Administrator as AdminUser;
class Administrator extends AdminUser
{
public function __construct( array $attributes = [] ) {
array_push( $this->fillable, 'park_id' );
parent::__construct( $attributes );
}
//定义模型关联关系
public function park()
{
return $this->belongsTo(Park::class);
}
}
新建一个controller
<?php
namespace App\Admin\Controllers;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\UserController;
use Dcat\Admin\Http\Repositories\Administrator;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Models\Administrator as AdministratorModel;
use Illuminate\Support\Facades\Log;
class AdministratorController extends UserController
{
protected function grid()
{
//这里使用我定义的
return Grid::make(\App\Models\Administrator::with(['roles','park']), function (Grid $grid) {
$grid->column('id', 'ID')->sortable();
$grid->column('username');
$grid->column('name');
if (config('admin.permission.enable')) {
$grid->column('roles')->pluck('name')->label('primary', 3);
$permissionModel = config('admin.database.permissions_model');
$roleModel = config('admin.database.roles_model');
$nodes = (new $permissionModel())->allNodes();
$grid->column('permissions')
->if(function () {
return ! $this->roles->isEmpty();
})
->showTreeInDialog(function (Grid\Displayers\DialogTree $tree) use (&$nodes, $roleModel) {
$tree->nodes($nodes);
foreach (array_column($this->roles->toArray(), 'slug') as $slug) {
if ($roleModel::isAdministrator($slug)) {
$tree->checkAll();
}
}
})
->else()
->display('');
}
$grid->column( 'park.park', '园区' );
$grid->column('created_at');
$grid->column('updated_at')->sortable();
$grid->quickSearch(['id', 'name', 'username']);
$grid->showQuickEditButton();
$grid->enableDialogCreate();
$grid->showColumnSelector();
$grid->disableEditButton();
$grid->actions(function (Grid\Displayers\Actions $actions) {
if ($actions->getKey() == AdministratorModel::DEFAULT_ID) {
$actions->disableDelete();
}
});
});
}
public function form() {
$f = parent::form();
$f->text( 'park_id' );
return $f;
}
public function update($id)
{
Log::info("这里编辑");
return $this->form()->update($id);
}
}
重新定义路由
$router->resource('auth/users', \App\Admin\Controllers\AdministratorController::class)->middleware(config('admin.route.middleware'));
二,修改架构为frame版(暂时有bug,不要使用)
composer require mosiboom/dcat-iframe-tab
# 发布扩展必备文件
$ php artisan vendor:publish --tag=iframe-tab
# 发布扩展配置文件
$ php artisan vendor:publish --tag=iframe-tab.config
修改 app\routes.php
...
$router->get('home', 'HomeController@index');
...
修改菜单中的路由
三,重写内置方法
可能会需要重写 update方法,store方法,delete方法,可在对应的controller中重写方法覆盖父类中的方法
public function update($id)
{
abort(403, '不让编辑');
Log::info("这里编辑了");
return parent::update($id);
}
public function store()
{
abort(403, '这里添加了');
Log::info("这里添加了");
return parent::store()
}
public function destroy($id)
{
abort(403, '不让删除');
Log::info("不让删除");
return parent::destroy($id);
}
四,自定义方法
自定义方法须要自己编写Action类
例如:我在index方法中禁用了默认的删除操作按钮,并重新定义了一个按钮newdelete
public function index(Content $content)
{
return $content->header('树状模型')
->body(function (Row $row) {
$tree = new Tree(new Classify);
$tree->branch(function ($branch) {
//$src = config('admin.upload.host') . '/' . $branch['logo'] ;icon-align-justify
$logo = "<i class='fa fa-fw feather icon-align-justify'></i>";
return "{$branch['id']} - {$branch['title']} $logo";
});
$tree->actions(function (Tree\Actions $actions) {
// 禁用默认删除操作
$actions->disableDelete();
});
//自定义删除操作
$tree->actions(new NewDelete());
//自定义操作
$tree->actions(new ShowList());
$row->column(12, $tree);
});
}