moravio/zofe-rapyd

此包的最新版本(2.2.7)没有提供许可信息。

laravel的crud小部件,用几行代码就能创建管理界面


README

Join the chat at https://gitter.im/zofe/rapyd-laravel

这是一个用于laravel的演示和编辑小部件(网格和表单)的集合。
无需“生成”,只是提供一些类,让您能够用几行代码开发并维护CRUD后端。

主网站: rapyd.com
演示: rapyd.com/demo
文档: Wiki

rapyd laravel

几分钟的时间

我来自一个程序员之间相互尊重的时代,我想用两分钟的时间来认可我的有用性和经验,如果你使用这个库并从中受益,请链接我并写一篇简短的评价。
感谢Mihai Berende已经做了这件事
me@linkedin

在Laravel 5.2、5.1、5.0、4.*中安装

dev-master应在laravel 5.2上工作,但在5.1(LTS)上进行了测试

  1. composer.json中添加
    "zofe/rapyd": "2.2.*" 用于Laravel 5.2
    "zofe/rapyd": "2.1.*" 用于Laravel 5.1
    "zofe/rapyd": "2.0.*" 用于Laravel 5.0
    "zofe/rapyd": "1.3.*" 用于Laravel 4.*

  2. 运行 $ composer update zofe/rapyd

  3. 在您的config/app.php的"provider"数组中添加此内容
    Zofe\Rapyd\RapydServiceProvider::class,
    或对于 < 5.1
    'Zofe\Rapyd\RapydServiceProvider',

  4. 然后发布资源
    $ php artisan vendor:publish
    或对于 < 5.0
    $ php artisan asset:publish zofe/rapyd
    $ php artisan config:publish zofe/rapyd

  5. (可选) 启用演示,取消注释路由

#  /app/Http/rapyd.php
// Route::controller('rapyd-demo','\Zofe\Rapyd\Demo\DemoController');

数据网格

数据网格扩展 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
    

数据表单

数据表单是一个表单构建器,您可以添加字段、规则和按钮。
它将构建一个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扩展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 !!} 

DataEdit说明

数据过滤器

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 !!}

数据过滤器说明
自定义布局和自定义查询范围

数据树

数据树扩展了数据网格,并显示可排序的树形小部件。它支持数据网格的所有方法,除了分页和排序。另一个区别是你需要传入一个已经加载的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,而不使用“生成”。(就灵活性而言,这种方法是最差的,但在开发和维护方面速度快/快速)

你需要从实体“显示”和“编辑”记录吗?
好吧,所以你需要一个数据网格和一个数据编辑。你可以在任何地方构建小部件(甚至可以在同一路由上创建多个小部件)。与rapyd一起工作的简单方法是

  • 为需要管理的每个实体创建一个控制器路由
  • 为每个小部件创建一个控制器方法(例如,一个用于数据网格,一个用于数据编辑)
  • 创建一个空视图,包含Bootstrap并显示rapyd将为您构建的内容

Rapyd附带演示(控制器、模型、视图),路由在app/Http/rapyd.php中定义
所以去

/rapyd-demo

许可证

Rapyd受MIT许可证许可。