shaburov / laravel-crud-rest-api
用于创建其他CRUD控制器的Crud Rest Api包。
v1.1.0
2022-12-16 08:02 UTC
Requires
- php: ^8.0
Requires (Dev)
- laravel/framework: ^9.0
- nunomaduro/collision: ^6.2
- orchestra/testbench: ^7.15.0
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-16 12:06:56 UTC
README
简介和安装
此包是为了创建快速CRUD操作而创建的,例如分页、无分页的对象列表、存储、更新、销毁和显示。
composer require shaburov/laravel-crud-rest-api
控制台
Publish config
php artisan vendor:publish --provider="CrudRestApi\CrudServiceProvider"
Run controller tests
php artisan crud-rest-api:test
Run migrations
php artisan crud-rest-api:migrate {param_from_migrate}
您可以在配置中激活默认路由,并查看其工作情况 load_routes => true (默认为false),然后执行命令 php artisan route:list。
如何使用
创建控制器并从CrudBaseController扩展,或者根据接口和特性创建自己的控制器,并使用方法setModelClass连接模型。
abstract class MyController extends CrudController implements
CrudSaveInterface, CrudIndexInterface,
CrudStoreInterface, CrudListInterface,
CrudUpdateInterface, CrudDestroyInterface,
CrudRestoreInterface, CrudShowInterface
{
use CrudListTrait,
CrudIndexTrait,
CrudStoreTrait,
CrudIndexTrait,
CrudUpdateTrait,
CrudRestoreTrait,
CrudDestroyTrait,
CrudSaveTrait,
CrudShowTrait;
abstract public function setModelClass(): string;
}
class ArticleController extends MyController
{
public function setModelClass(): string
{
return Article::class;
}
}
验证
您应该使用CrudValidatorInterface并使用Trait CrudValidatorTrait,然后在方法setValidations中连接验证。
public function setValidations(): void
{
$this->validateShow = ArticleShowRequest::class;
$this->validateStore = ArticleStoreRequest::class;
$this->validateIndex = null;
$this->validateList = null;
$this->validateUpdate = null;
$this->validateDelete = null;
}
更改行为
All methods include in yourself methods
for change default behaviors
public function beforeList();
public function afterList();
public function updating()
public function updated($model)
public function creating()
public function created($model)
public function saving()
public function saved($model)
public function beforeIndex()
public function afterIndex()
public function beforeShow()
public function afterShow()
public function deleting()
public function deleted()
路由
您应该在路由文件中添加路由。
Route::namespace('App\Http\Controllers')->group(function (){
Route::resource('articles', 'ArticleController');
});
参数
index [GET] include parameters
page - current page // articles?page=1
per_page - number of objects in the list // articles?per_page=1
list - for get all object // articles?list
您可以使用 $this->request 并在行为中处理它。
public function beforeIndex(): void
{
$title = $this->request->get('title');
$this->state
->select(['id','title','category_id',])
->where('title', $title)
->with('category:id,title');
}
or use $this->requestData and get parameter and rewrite it
public function updating(): void
{
$this->requestData['title'] = Str::lower($this->requestData['title']);
}
配置
'load_routes' => false,
'migration_dir' => database_path()."/migrations/crud" // path export migrations - not required ,
'per_page' => [
'key' => 'per_page', // key for get parameter
'value' => 10, // default value
'limit' => 100, // max value
],