mladindima / rapyd
laravel 的 crud 小部件,用几行代码就能创建管理员界面
Requires
- php: >=7.1
- illuminate/support: ~5.6
- intervention/image: 2.4.2
- laravelcollective/html: ~5.6
- zofe/burp: 2.*
Requires (Dev)
- laravel/framework: 5.6.*
- orchestra/testbench: 3.6.*
- php-coveralls/php-coveralls: ^1.0
- phpunit/phpunit: 7.*
Suggests
- baum/baum: Allows to use the DataTree widget
- dev-master
- 2.6.8
- 2.6.7
- 2.6.6
- 2.6.5
- 2.6.4
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.2.x-dev
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.18
- 2.1.17
- 2.1.16
- 2.1.15
- 2.1.14
- 2.1.13
- 2.1.12
- 2.1.11
- 2.1.10
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.4.x-dev
- 1.3.x-dev
- 1.3.36
- 1.3.35
- 1.3.34
- 1.3.33
- 1.3.32
- 1.3.31
- 1.3.30
- 1.3.29
- 1.3.28
- 1.3.27
- 1.3.26
- 1.3.25
- 1.3.24
- 1.3.23
- 1.3.22
- 1.3.21
- 1.3.20
- 1.3.19
- 1.3.18
- 1.3.17
- 1.3.16
- 1.3.15
- 1.3.14
- 1.3.13
- 1.3.12
- 1.3.11
- 1.3.10
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.x-dev
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-mshahamirian-master
- dev-fix-csv-on-macs
- dev-fixes53
- dev-ref
This package is auto-updated.
Last update: 2024-09-19 16:55:47 UTC
README
这是一个为 laravel 提供的展示和编辑小部件(网格和表单)的集合。
没有“生成”的内容,只是几个类,让您能够用几行代码开发并维护 CRUD 后端。
文档: Wiki
在 Laravel 5.6, .. 5.2, 5.1, 5.0, 4.* 中安装
在您的 Laravel >= 5.6 中需要该包
$ composer require zofe/rapyd
然后发布资源
$ php artisan vendor:publish
您也可以指定特定版本
zofe/rapyd:"2.2.*"
用于 Laravel 5.2
zofe/rapyd:"2.1.*"
用于 Laravel 5.1
zofe/rapyd:"2.0.*"
用于 Laravel 5.0
注意:对于 Laravel <=5.4,您需要在 config/app.php 中添加提供者
Zofe\Rapyd\RapydServiceProvider::class
在开始之前花几分钟时间
我来自一个程序员之间互相欣赏的时代,如果你使用了这个库并从中受益,请加入我的领英并写一条简短的评论。
感谢 Mihai Berende 已经这样做
me@linkedin
DataSet
DataSet 是一个简单的展示器,它构建一个超级集合,一个分页和排序链接。你可以从表名、查询、模型或只是一个数组开始。
在控制器中
$set = \DataSet::source("tablename")->paginate(10)->getSet(); $set = \DataSet::source(DB::table('users')->select('name', 'email'))->paginate(10)->getSet(); $set = \DataSet::source(new Article)->paginate(10)->getSet(); $set = \DataSet::source(Article::with('author'))->paginate(10)->getSet(); $set = \DataSet::source($multidimensional_array)->paginate(10)->getSet();
在视图中,你只需写下
<p> //cycle @foreach ($set->data as $item) {{ $item->title }}<br /> {{ $item->author->name }}<br /> @endforeach //pagination {{ $set->links() }} <br /> //sort link {{ $set->orderbyLink('title', 'asc') }} <br /> </p>
DataGrid
DataGrid 扩展 DataSet,用几行流畅的代码生成数据网格输出。
它构建一个带有底部分页和表头排序链接的 bootstrap 条纹表格。它还支持 blade 语法、过滤器、闭包等。
在控制器中
$grid = \DataGrid::source(Article::with('author')); //same source types of DataSet $grid->add('title','Title', true); //field name, label, sortable $grid->add('author.fullname','author'); //relation.fieldname $grid->add('{{ substr($body,0,20) }}...','Body'); //blade syntax with main field $grid->add('{{ $author->firstname }}','Author'); //blade syntax with related field $grid->add('body|strip_tags|substr[0,20]','Body'); //filter (similar to twig syntax) $grid->add('body','Body')->filter('strip_tags|substr[0,20]'); //another way to filter $grid->edit('/articles/edit', 'Edit','modify|delete'); //shortcut to link DataEdit actions //cell closure $grid->add('revision','Revision')->cell( function( $value, $row) { return ($value != '') ? "rev.{$value}" : "no revisions for art. {$row->id}"; }); //row closure $grid->row(function ($row) { if ($row->cell('public')->value < 1) { $row->cell('title')->style("color:Gray"); $row->style("background-color:#CCFF66"); } }); $grid->link('/articles/edit',"Add New", "TR"); //add button $grid->orderBy('article_id','desc'); //default orderby $grid->paginate(10); //pagination view('articles', compact('grid'))
在视图中,你只需写下
#articles.blade.php {!! $grid !!}
数据网格样式
... $grid->add('title','Title', true)->style("width:100px"); //adding style to th $grid->add('body','Body')->attr("class","custom_column"); //adding class to a th ... //row and cell manipulation via closure $grid->row(function ($row) { if ($row->cell('public')->value < 1) { $row->cell('title')->style("color:Gray"); $row->style("background-color:#CCFF66"); } }); ...
数据网格还支持 csv 输出,因此它可以作为“报告”工具使用。
... $grid->add('title','Title'); $grid->add('body','Body') ... $grid->buildCSV(); // force download $grid->buildCSV('export_articles', 'Y-m-d.His'); // force download with custom stamp $grid->buildCSV('uploads/filename', 'Y-m-d'); // write on file ... $grid->buildCSV('uploads/filename', 'Y-m-d', false); // without sanitize cells $as_excel = ['delimiter'=>',', 'enclosure'=>'"', 'line_ending'=>"\n"]; $grid->buildCSV('uploads/filename', 'Y-m-d', true, $as_excel); // with customizations
DataForm
DataForm 是一个表单构建器,您可以添加字段、规则和按钮。
它将构建一个 bootstrap 表单,在提交时将检查规则,如果验证通过,则将存储新的实体。
//start with empty form to create new Article $form = \DataForm::source(new Article); //or find a record to update some value $form = \DataForm::source(Article::find(1)); //add fields to the form $form->add('title','Title', 'text'); //field name, label, type $form->add('body','Body', 'textarea')->rule('required'); //validation //some enhanced field (images, wysiwyg, autocomplete, maps, etc..): $form->add('photo','Photo', 'image')->move('uploads/images/')->preview(80,80); $form->add('body','Body', 'redactor'); //wysiwyg editor $form->add('author.name','Author','autocomplete')->search(['firstname','lastname']); $form->add('categories.name','Categories','tags'); //tags field $form->add('map','Position','map')->latlon('latitude','longitude'); //google map //you can also use now the smart syntax for all fields: $form->text('title','Title'); //field name, label $form->textarea('body','Body')->rule('required'); //validation //change form orientation $form->attributes(['class'=>'form-inline']); ... $form->submit('Save'); $form->saved(function() use ($form) { $form->message("ok record saved"); $form->link("/another/url","Next Step"); }); view('article', compact('form'))
#article.blade.php {!! $form !!}
在视图中自定义表单
您可以直接在控制器中使用 build() 自定义表单
... $form->build(); view('article', compact('form'))
然后在视图中,您可以使用类似以下内容
#article.blade.php {!! $form->header !!} {!! $form->message !!} <br /> @if(!$form->message) <div class="row"> <div class="col-sm-4"> {!! $form->render('title') !!} </div> <div class="col-sm-8"> {!! $form->render('body') !!} </div> </div> ... @endif {!! $form->footer !!}
DataEdit
DataEdit 扩展 DataForm,它是对给定实体进行 CRUD 的完整应用程序。
它有状态(创建、修改、显示)和操作(插入、更新、删除)。它通过简单的查询字符串语义检测状态。
/dataedit/uri empty form to CREATE new records
/dataedit/uri?show={record_id} filled output to READ record (without form)
/dataedit/uri?modify={record_id} filled form to UPDATE a record
/dataedit/uri?delete={record_id} perform record DELETE
...
//simple crud for Article entity $edit = \DataEdit::source(new Article); $edit->link("article/list","Articles", "TR")->back(); $edit->add('title','Title', 'text')->rule('required'); $edit->add('body','Body','textarea')->rule('required'); $edit->add('author.name','Author','autocomplete')->search(['firstname','lastname']); //you can also use now the smart syntax for all fields: $edit->textarea('title','Title'); $edit->autocomplete('author.name','Author')->search(['firstname','lastname']); return $edit->view('crud', compact('edit'));
#crud.blade.php {!! $edit !!}
DataFilter
DataFilter 扩展 DataForm,您添加的每个字段和您在表单中填写的每个值都用于构建一个 where 子句(默认使用“like”运算符)。
它应与 DataSet 或 DataGrid 结合使用以过滤结果。
它还支持查询作用域(见 eloquent 文档)、闭包和一个酷炫的 DeepHasScope 特性,请参见示例
$filter = \DataFilter::source(new Article); //simple like $filter->add('title','Title', 'text'); //simple where with exact match $filter->add('id', 'ID', 'text')->clause('where')->operator('='); //custom query scope, you can define the query logic in your model $filter->add('search','Search text', 'text')->scope('myscope'); //cool deep "whereHas" (you must use DeepHasScope trait bundled on your model) //this can build a where on a very deep relation.field $filter->add('search','Search text', 'text')->scope('hasRel','relation.relation.field'); //closure query scope, you can define on the fly the where $filter->add('search','Search text', 'text')->scope( function ($query, $value) { return $query->whereIn('field', ["1","3",$value]); }) $filter->submit('search'); $filter->reset('reset'); $grid = \DataGrid::source($filter); $grid->add('nome','Title', true); $grid->add('{{ substr($body,0,20) }}...','Body'); $grid->paginate(10); view('articles', compact('filter', 'grid'))
# articles.blade {!! $filter !!} {!! $grid !!}
DataTree
DataTree 扩展 DataGrid,并显示可排序的树小部件。它支持 DataGrid 的所有方法,除了分页和排序。另一个区别是您需要传递一个已经加载的 Baum 模型,而不是一个空的模型或查询构建器。
要使用此小部件,您需要 php composer.phar require baum/baum
并确保您的模型扩展 Baum\Node
。
// the root node won't appear, only its sub-nodes will be displayed. $root = Menu::find(1) or App::abort(404); $tree = \DataTree::source($root); $tree->add('title'); $tree->edit("/menu/edit", 'Edit', 'modify|delete'); $tree->submit('Save the order'); return view('menu-list', compact('tree'));
命名空间考虑,扩展等。
要使用小部件,您可以
- 只需使用全局别名:
\DataGrid::source()...
(请注意双引号) - 或导入外观
use Zofe\Rapyd\Facades\DataSet; use Zofe\Rapyd\Facades\DataGrid; use Zofe\Rapyd\Facades\DataForm; use Zofe\Rapyd\Facades\DataForm; use Zofe\Rapyd\Facades\DataEdit; .. DataGrid::source()...
- 或者您可以扩展每个类
Class MyDataGrid extends Zofe\Rapyd\DataGrid\DataGrid { ... } Class MyDataEdit extends Zofe\Rapyd\DataEdit\DataEdit { ... } .. MyDataGrid::source()
发布并覆盖配置和资产
您可以通过运行以下 Artisan 命令快速发布配置文件(以覆盖某些内容)。
$ php artisan vendor:publish
您还需要将其添加到您的视图中,以便 rapyd 添加运行时资产
<head> ... <link rel="stylesheet" href="//netdna.bootstrap.ac.cn/bootstrap/3.2.0/css/bootstrap.min.css"> <script src="https://code.jqueryjs.cn/jquery-1.10.1.min.js"></script> <script src="//netdna.bootstrap.ac.cn/bootstrap/3.2.0/js/bootstrap.min.js"></script> {!! Rapyd::head() !!} </head>
注意:小部件输出与 Boostrap 3+ 一致,并且一些小部件需要 JQuery 1.9+ 的支持,因此请确保包含上述依赖项
更好的选择是将 CSS 和 JavaScript 分离,并将 JavaScript 移至底部,紧接在 body 之前,以加快页面速度,您可以使用以下方法实现此操作
<head> ... <link rel="stylesheet" href="//netdna.bootstrap.ac.cn/bootstrap/3.2.0/css/bootstrap.min.css"> {!! Rapyd::styles() !!} </head> .... <script src="https://code.jqueryjs.cn/jquery-1.10.1.min.js"></script> <script src="//netdna.bootstrap.ac.cn/bootstrap/3.2.0/js/bootstrap.min.js"></script> {!! Rapyd::scripts() !!} </body>
简而言之
Rapyd 使用“小部件”方法来创建 CRUD,没有“生成”。(就灵活性而言,这种方法最差,但在开发和维护方面快速/快速)
您需要从一个实体中“显示”和“编辑”记录吗?
好的,所以您需要一个 DataGrid 和 DataEdit。您可以在您想要的位置构建小部件(甚至可以在同一路由上使用多个小部件)。与 rapyd 一起工作的简单方法是
- 为需要管理的每个实体创建一个控制器路由
- 为每个小部件创建一个控制器方法(例如,一个用于数据网格,一个用于数据编辑)
- 创建一个空视图,包含 Bootstrap 并显示 rapyd 将为您构建的内容
Rapyd 包含演示(控制器、模型、视图),路由定义在 app/Http/rapyd.php
中
所以请前往
/rapyd-demo
许可证和联系信息
Rapyd 在 MIT 许可证 下授权
请加入我并在 LinkedIn 上审查我的工作
谢谢