前言
两种后台管理扩展我都使用过,两个扩展既有很多相似点,却又有很多不同。接触过laravel nova之后,再接触laravel admin有种莫名的熟悉感,发现在开发中有很多类似的设计思路。
接触laravel admin时间只有一个来月,时间还不长,随便写写吧。有不足之处还请多多指教。
感受
其实laravel nova就是laravel admin的进化版,显然laravel admin是开源扩展,而laravel nova是付费使用的(99刀一个授权可不便宜),在这不考量两者使用授权方面。
laravel admin是一个中国开发者,做出这样的扩展确实非常了不起。扩展设计合理,但是优化上没有laravel nova那么好。
其实最开始接触laravel nova之前是考虑过laravel admin的,由于laravel admin已经封装好了权限管理,自己替换处理又相对麻烦(其实就是懒),才放弃的。laravel admin的权限管理没有做缓存处理,每次使用都要从数据库查询,其实完全可以缓存起来减轻数据库压力。相对于laravel nova这个功能已经交给开发者实现(我还写过一篇关于 laravel nova 权限管理 的文章)。类似于这样需要缓存的还有菜单功能,因为网站上线后这些几乎不变的的数据却又频繁读取,缓存能解决不少数据库压力(有时间自己处理下)。
laravel admin模板还是使用HTML(对于我这种前端弱的一逼的程序员来说真是非常友好),而laravel nova使用vue.js,相对来说vue.js体验感更好,但是技术要求也相对较高。折腾vue.js多多少少还是要翻几次文档。在扩展字段的时候前端模板就相当重要了(反正我写的CSS无法直视)。
laravel admin更加符合国人操作习惯,我使用admin LTE开发过几个后台,基本上不需要教如何使用,大多数功能、按钮一目了然。而laravel nova很多功能、按钮藏的比较深,很多同事反映过找不到地方...
在代码层面,laravel admin我总感觉没有laravel nova那么简洁、灵活。列表页,详情页,编辑页总共需要些三个不同的模板,我是用laravel localization本地化功能,所以需要将三个模板全部修改翻译,而laravel nova只需要编写一次模板,不需要在某个页面显示的字段可以单独关闭:
//laravel admin 列表页、详情页、表单,其实还需要三个控制器动作分别输出不同的模板
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new ProductSpecification);
$this->gridViewPermission($grid);
$grid->id('Id')->sortable();
$grid->name(trans('product/specification.name'))->sortable();
$grid->created_at(trans('admin.created_at'))->sortable();
$grid->updated_at(trans('admin.updated_at'))->sortable();
return $grid;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(ProductSpecification::findOrFail($id));
$this->showViewPermission($show);
$show->id('Id');
$show->name(trans('product/specification.name'));
$show->attributes(trans('product/specification_attribute.index'),function (Grid $attributes){
$this->gridViewPermission($attributes);
$attributes->setResource(route('admin.product.specification.attribute.index'));
$attributes->id()->sortable();
$attributes->name(trans('product/specification_attribute.name'))->sortable();
$attributes->created_at(trans('admin.created_at'))->sortable();
$attributes->updated_at(trans('admin.updated_at'))->sortable();
});
$show->created_at(trans('admin.created_at'));
$show->updated_at(trans('admin.updated_at'));
return $show;
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new ProductSpecification);
$this->formViewPermission($form);
$form->text('name', trans('product/specification.name'))
->rules('unique:product_specifications')
->required();
$form->hasMany('attributes', trans('product/specification_attribute.index'), function (Form\NestedForm $form){
$form->text('name',trans('product/specification_attribute.name'))->required();
});
$form->saved(function (Form $form){
$form->model()->forgetCache();
});
return $form;
}
...
//laravel nova 表单、列表、详情
public function fields(Request $request)
{
$guardOptions = collect(config('auth.guards'))->mapWithKeys(function ($value, $key) {
return [$key => __('nova-permission-tool::roles.guard_names.'.$key)];
});
$userResource = Nova::resourceForModel(getModelForGuard($this->guard_name));
return [
ID::make()->sortable(),
Text::make(__('nova-permission-tool::roles.name'), 'name')
->rules(['required', 'string', 'max:255'])
->creationRules('unique:'.config('permission.table_names.roles'))
->updateRules('unique:'.config('permission.table_names.roles').',name,{{resourceId}}'),
Text::make(__('nova-permission-tool::roles.guard_name'), function () {
return __('nova-permission-tool::roles.guard_names.'.$this->guard_name);
}),
Select::make(__('nova-permission-tool::roles.guard_name'), 'guard_name')
->options($guardOptions->toArray())
->rules(['required', Rule::in(array_keys($guardOptions->toArray()))])
->onlyOnForms(),
DateTime::make(__('nova-permission-tool::roles.created_at'), 'created_at')->exceptOnForms(),
DateTime::make(__('nova-permission-tool::roles.updated_at'), 'updated_at')->exceptOnForms(),
BelongsToMany::make(__('nova-permission-tool::resources.Permissions'), 'permissions', Permission::class)->searchable(),
MorphToMany::make($userResource::label(), 'users', $userResource)->searchable(),
];
}
通过以上代码对比可以看出两个扩展在代码简洁性上有蛮大区别的。一个个字段写入翻译蛮麻烦的。在关系字段处理上需要关联表的控制器,然后增删改查又是一套...
当然这样也是有好处的,高度自定义,只不过一个后台管理用处不多,大多数可以自定义字段扩展出来。
laravel admin几乎把所有字段所需的 javaScript放在后端输出,其实这样对前端不友好,现在都前后端开始分离了,前端代码放在PHP中嵌套写蛮乱的,后期维护也相对困难。
结语
laravel nova和laravel admin都是非常好的扩展,只是国内大多数只知道拿人家的用,却很少去贡献,生态不如国外。两个扩展都朝着相同的目标,节省后台开发时间,但是一个收费,一个开源,而且laravel nova还是laravel作者团队开发的(框架加持)。扩展都是好扩展,要按项目需求和自己的熟练程度来选择使用哪种扩展开发后台,有能力两种都试试,深深体会一下。
评论 (0)