aluna / laracrud
Laravel的CRUD
0.0.26
2016-06-24 23:58 UTC
Requires
- php: >=5.3.0
- venturecraft/revisionable: 1.*
README
官方文档
该包的文档可以在LaraCrud网站找到。
文档
使用composer安装包
composer require aluna/laracrud
在config/app.php文件中的providers数组中添加以下条目
LaraCrud\LaraCrudServiceProvider::class,
第1步
创建一个新的模型,编辑最近创建的模块并包含
<?php namespace App; use Illuminate\Database\Eloquent\Model; use LaraCrud\LaraCrudModel; class ExampleModel extends Model { /** * Model columns to be hidden on views * @var array */ protected $hidden = ['created_at', 'updated_at']; /** * Laravel Mass Assigment protection * @var array */ protected $fillable = ['column1', 'column2']; /** * LaraCrud Model trait */ use LaraCrudModel; }
第2步
控制器必须看起来像这样
<?php namespace App\Http\Controllers; use App\ExampleModel; use Illuminate\Http\Request; use LaraCrud\LaraCrudController; class ExampleController extends Controller { /** * LaraCrud Controller Trait */ use LaraCrudController; /** * This variable will store the Model related to this controller. * @var Model */ protected $model; /** * This is the base route that will be used with REST methods * @type String */ protected $route = '/example'; /** * Name that will be displayed on form headers, session messages, pop up messages, etc. * @type String */ protected $crudName = 'Example'; /** * If you want to overwrite the default views of the package * @type String */ protected $views = 'vendor.laracrud'; /** * The attributes that are available in index view. * * @var array */ protected $displayable = [ 'column1', 'column2' ]; /** * Dispatch events on resource events * * @type array */ protected $events = [ 'store' => [ 'class' => 'App\Events\ExampleEvent', 'params' => ['result', 'request:password'] ] ]; /** * Let IoC Container to resolve Model and assing it to variable. * * @param Model ExampleModel $example */ public function __construct(ExampleModel $example) { $this->model = $example; } /** * Method called when post resource has been overwritten * eg: encrypt password before storing * * @param Illuminate\Http\Request Request $request * @return Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse */ public function modifyRequest(Request $request) { $request->merge([ 'password' => bcrypt($request->password) ]); return $this->store($request); } }
第3步
路由将如下所示
// Default Laravel Resource Route::resource('/example', 'ExampleController'); // Overwrite resource REST method to modify request then calling REST method Route::post('/example', 'ExampleController@modifyRequest');
额外
如果您想覆盖或修改默认视图,首先需要将视图发布到资源文件夹中
php artisan vendor:publish
视图将可在resources/vendor/laracrud中找到,您可以根据需要自由编辑它们。
贡献
感谢您考虑为LaraCrud包做出贡献!
许可
LaraCrud包是开源软件,许可协议为MIT许可