flo5581 / larabackend
laravel 的后端
Requires
- flostone/larahelpers: 1.3.*
- maatwebsite/excel: ~2.0.0
README
##安装
###将包添加到 composer.json
"flostone/larahelpers" : "1.0.*"
在 App\Console 中的 Kernel.php 中添加
\FloStone\Backend\Commands\AdminInstallation::class
###注册服务提供者
Flo\Backend\BackendProvider::class
Flo5581\Larahelpers\BladeExtensions::class
Maatwebsite\Excel\ExcelServiceProvider::class
###添加门面
'Excel' => Maatwebsite\Excel\Facades\Excel::class
执行
php artisan admin:install
此命令适用于 Laravel 5.0.*,但是您需要使用字符串而不是 ::class。
示例
'\FloStone\Backend\Commands\AdminInstallation'
##基本用法
在进行任何操作之前:创建一个扩展
FloStone\Backend\Controllers\AdminController
的控制器,并使用以下命令将控制器添加到您的 routes.php
Route::controller('admin', 'YourController')
或
controller('admin', 'YourController')
请记住使用中间件保护控制器,但是当您这样做时,您还需要调用父构造函数
public function __constrcut() {
parent::__construct();
``$this->middleware('admin);<br>
}`
首先,您需要告诉 Backend 哪些控制器方法应该显示在菜单中。键表示菜单中显示的名称,值表示控制器方法
public static $displayed_actions = ['Index' => 'getIndex']
要创建显示模型的视图,您必须使用父控制器中的 view 方法。不要使用 laravel 视图方法!只需传递您希望使用的模型即可。
public function getIndex()
{
$this->view(Model::class);
}
要向视图添加部分,例如一个应显示内容的表格,您需要使用 addTable 方法
$this->view(Model::class)->addTable()
这将在表格中自动打印出模型的数据。此外,您还可以提供作为第一个参数的自定义数据集合到表中,并且您可以决定内容是否可编辑,默认值为 true。
$this->view(Model::class)->addTable($custom_data, true)
最后,调用 render 方法使一切工作
$this->view(Model::class)->addTable()->render()
要定义模型中应显示在表格中的列,您必须添加
public static $displayed_columns = ['email', 'name']
到您的模型中,并将要显示的字段放入其中,以定义可以由管理员编辑的列,您需要添加
public static $editable_columns = ['email' => []]
此外,您还可以向该字段添加属性,例如设置作为表标题显示的标签
public static $editable_columns = ['email' => ['label' => 'E-Mail']]
请注意,如果没有此变量或变量为空,将导致错误
通过使用 addExport 方法添加导出字段,通过第一个参数传递文件类型
$this->view(Model::class)->addTable()->addExport('xls')->render()
##模型定义
以下是所有模型定义及其功能的列表
###1
public static $display_columns = ['column' => [(options)]]
字段必须设置!
后端表中所有显示列的数组
选项
'label' => '列名称'
'relation' => [(属性)]
关系属性
方法 => '关系方法名称'
显示 => '相关对象的显示字段'
###2
public static $editable_columns = ['列' => [(选项)]]
字段必须设置!
应可编辑的列数组
选项
'label' => '列名称'
类型 => '字段类型'
字段类型列表
字符串
文本
文本区域
整数
整型
密码
密码
复选框
布尔型
下拉列表
下拉框
枚举
文件
图片
###3
public static $export_fields = ['列' => [(选项)]]
字段可设置
应导出的字段数组,默认导出所有列和关系
选项
'label' => '列名称'
'relation' => [(属性)]
关系属性
方法 => '关系方法名称'
显示 => '相关对象的显示字段'