novius / laravel-backpack-crud-extended
此包扩展了 Laravel Backpack\CRUD
Requires
- php: >=7.1
- backpack/crud: ~3.4.0
- laravelcollective/html: ^5.5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.8.0
- orchestra/testbench: ~3.5
- phpunit/phpunit: ~6.0
README
此包扩展了 Backpack/CRUD。以下列出了所有新增的功能。
在不修改控制器或其他包的情况下完成此操作,此包
- 能够覆盖所有 Backpack/CRUD 视图;
- 扩展了 CrudPanel 类。
目录
安装
在您的终端
composer require novius/laravel-backpack-crud-extended
然后,如果您使用的是 Laravel 5.4(不需要 Laravel 5.5 及更高版本),将服务提供者注册到您的 config/app.php
文件
'providers' => [ ... Novius\Backpack\CRUD\CrudServiceProvider::class, ];
发布视图
如果您已经发布了 backpack-crud 视图,此包将无法正常工作。您必须从 resources/views/vendor/backpack/crud/
中删除视图,或者使用以下方式覆盖它们
php artisan vendor:publish --provider="Novius\Backpack\CRUD\CrudServiceProvider" --force
配置
您可以选择覆盖一些选项。
php artisan vendor:publish --provider="Novius\Backpack\CRUD\CrudServiceProvider" --tag="config"
使用 & 功能
权限
描述
- 权限可以自动应用于 CRUD 控制器:如果用户没有与路由链接的权限,则拒绝访问 =>
apply_permissions
选项 - 可以为应用程序中使用的所有 Crud 控制器自动创建权限(将创建 4 个权限:列表、更新、创建、删除)=>
create_permissions_while_browsing
选项 - 可以自动将权限授予登录用户(在本地环境中很有用)=>
give_permissions_to_current_user_while_browsing
选项
要求
- 此功能需要: Laravel-Backpack/PermissionManager
- 您必须发布配置文件
php artisan permissions:generate // Insert permissions in database for each CRUD controllers.
- 将每个要激活的选项设置为 true
使用方法
您的 CrudController 必须扩展 \Novius\Backpack\CRUD\Http\Controllers\CrudController
CRUD 盒子
现在,您可以将创建/编辑输入拆分为多个盒子。
为了使用此功能,您只需指定每个字段的盒子名称。
$this->crud->addField([ 'name' => 'title', 'label' => "My Title", 'type' => 'text', 'box' => 'Box name here' ]);
您还可以为每个盒子指定一些选项
$this->crud->setBoxOptions('Details', [ 'side' => true, // Place this box on the right side? 'class' => "box-info", // CSS class to add to the div. Eg, <div class="box box-info"> 'collapsed' => true, // Collapse this box by default? ]);
如果您忘记为字段指定标签名称,Backpack 将将其放置在最后一个盒子中。
您可以在 CRUD 文件中手动指定默认盒子。如果您的字段没有盒子属性,此字段将显示在此默认盒子中。
$this->crud->setDefaultBox('YourBoxName');
字段驱动
字段类型现在可以是类名
$this->crud->addField([ 'name' => 'username', 'label' => "My username", 'type' => \My\Other\Package\Field\Foo::class, ]);
这允许您在外部包中提出新的字段类型。您的 Field 类必须实现 Field Contract。
语言 / i18n
为特定 CRUD 设置自定义字典
$this->crud->setLangFile('backpack::crud/movie');
然后,此字典将在 CRUD 视图中使用。
您可以在自己的视图中这样使用它
{{ trans($crud->getLangFile().'.add') }}
上传字段:UploadableFile
特性
如果您使用 Upload CRUD 字段,您可以在您的模型上实现此特性来自动上传/删除服务器上的文件。
示例
// Article Model class Article extends \Backpack\NewsCRUD\app\Models\Article { use Sluggable, SluggableScopeHelpers; use HasTranslations; use UploadableFile; protected $fillable = ['slug', 'title', 'content', 'image', 'status', 'category_id', 'featured', 'date', 'document', 'document_2']; protected $translatable = ['slug', 'title', 'content']; public function uploadableFiles(): array { return [ ['name' => 'document'], ['name' => 'document_2', 'slug' => 'title'] ]; } }
// ArticleCrudController $this->crud->addField([ 'label' => 'Image', 'name' => 'image', 'type' => 'image', 'upload' => true, 'crop' => true, // set to true to allow cropping, false to disable 'aspect_ratio' => 0, // ommit or set to 0 to allow any aspect ratio 'prefix' => '/storage/', ]); $this->crud->addField([ 'label' => 'Document', 'name' => 'document', 'type' => 'upload', 'upload' => true, 'prefix' => '/storage/', ]); $this->crud->addField([ 'label' => 'Document 2', 'name' => 'document_2', 'type' => 'upload', 'upload' => true, 'prefix' => '/storage/', ]);
上传字段:file_upload_crud
验证规则
存在一个验证规则,可以轻松验证带有“上传”字段的 CRUD 请求。
在您的请求文件中使用示例
public function rules() { return [ 'name' => 'required|min:2|max:191', 'document' => 'file_upload_crud:pdf,docx', // the parameters must be valid mime types ]; } public function messages() { return [ 'file_upload_crud' => 'The :attribute must be a valid file.', ]; }
图片字段:UploadableImage
特性
如果您使用 Image CRUD 字段,您可以在您的模型上实现此特性,以自动管理在服务器上保存和删除图片。
示例
namespace App\Models; use Backpack\CRUD\CrudTrait; use Illuminate\Database\Eloquent\Model; use Novius\Backpack\CRUD\ModelTraits\UploadableImage; class Example extends Model { use CrudTrait; use UploadableImage; protected $fillable = ['title', 'image', 'thumbnail']; public function uploadableImages() { return [ [ 'name' => 'image', // The attribute name where to store the image path 'slug' => 'title', // The attribute name from which to generate the image file name (optionnal) ], [ 'name' => 'thumbnail', ], ]; } }
如果您想在保存或删除图片后执行一些自定义操作
namespace App\Models; use Backpack\CRUD\CrudTrait; use Illuminate\Database\Eloquent\Model; use Novius\Backpack\CRUD\ModelTraits\UploadableImage; class Example extends Model { use CrudTrait; use UploadableImage { imagePathSaved as imagePathSavedNative; imagePathDeleted as imagePathDeletedNative; } protected $fillable = ['title', 'image', 'thumbnail']; public function uploadableImages() { return [ [ 'name' => 'image', // The attribute name where to store the image path 'slug' => 'title', // The attribute name from which to generate the image file name (optionnal) ], [ 'name' => 'thumbnail', ], ]; } /** * Callback triggered after image saved on disk */ public function imagePathSaved(string $imagePath, string $imageAttributeName = null, string $diskName = null) { if (!$this->imagePathSavedNative()) { return false; } // Do what you want here return true; } /** * Callback triggered after image deleted on disk */ public function imagePathDeleted(string $imagePath, string $imageAttributeName = null, string $diskName = null) { if (!$this->imagePathDeletedNative()) { return false; } // Do what you want here return true; } }
媒体库
如果您想使用Spatie提供的MediaLibrary存储图片,请使用特性SpatieMediaLibrary\UploadableImage
而不是UploadableImage
。
例如,使用媒体库可以轻松管理转换(裁剪、调整大小等)。
翻译
UploadableImage
和SpatieMediaLibrary\UploadableImage
特性都兼容Spatie提供的翻译包。
在使用SpatieMediaLibrary\UploadableImage
的可翻译图片的情况下,存储图片的集合名称由属性名称和区域设置名称组成,用短横线分隔(例如image-en
、image-fr
等)。
CRUD:自定义路由
您可以为某些路由设置自定义值。
- 索引路由:与“返回所有”按钮或面包屑链接一起使用。使用
$crud->indexRoute()
可获得。 - 重新排序路由:与“重新排序按钮”一起使用。使用
$crud->reorderRoute()
可获得。
在您的CrudController中的使用示例
// Set a custom index route $this->crud->setIndexRoute('crud.slide.index', ['slideshow' => (int) request('slideshow')]); // Set a custom reorder route $this->crud->setReorderRoute('crud.slide.reorder', ['slideshow' => (int) request('slideshow')]);
测试
使用以下命令运行测试
./test.sh
代码风格检查
使用php-cs运行
./cs.sh
贡献
欢迎贡献!在Github上提交问题或创建Pull Request。
许可证
此软件包受GNU Affero通用公共许可证v3或(根据您的选择)任何后续版本的保护。
然而,此软件包需要Backpack\CRUD,该软件包受YUMMY许可证的保护:如果您将其用于商业项目,您必须购买Backpack许可证。