yaangvu/laravel-base
最新稳定版本:v4.1.3
Composer 安装命令:
composer require yaangvu/laravel-base
包简介
Laravel API Resource Base
README 文档
README
This base will help to create simple API (CRUD) for 1 specific entity
Install
composer require yaangvu/laravel-base
For Laravel
Publish configuration file and Base Classes
php artisan vendor:publish --provider="YaangVu\LaravelBase\Providers\BaseServiceProvider"
For lumen
cp vendor/yaangvu/laravel-base/src/config/laravel-base.php config/laravel-base.php mkdir -p app/Base cp vendor/yaangvu/laravel-base/src/Base/Publish/Controller.php app/Base/Controller.php cp vendor/yaangvu/laravel-base/src/Base/Publish/Service.php app/Base/Service.php
Generator Command
If you want to use Generator Command, Add the following class to the providers array in config/app.php:
YaangVu\LaravelBase\Provider\GeneratorServiceProvider::class,
If you want to manually load it only in non-production environments, instead you can add this to
your AppServiceProvider with the register() method:
public function register() { if ($this->app->isLocal()) { $this->app->register(\YaangVu\LaravelBase\Provider\GeneratorServiceProvider::class); } // ... }
Initial API resource
Generate code
php artisan yaangvu:base Post <option>
Option:
- -S: generate code with default Swagger annotation
- -i: Auto inject Service in Controller methods
Directory structure of generated code
├── app
│ ├── Domains
│ │ └── Post
│ │ ├── Controllers
│ │ │ └── PostController.php
│ │ ├── Models
│ │ │ └── Post.php
│ │ └── Services
│ │ └── PostService.php
Route
Route::base('/posts', \App\Domains\Post\Controllers\PostController::class);
Usage
Dynamic query parameters
Operators supported
$operators
= [
'__gt' => OperatorConstant::GT, // Greater than
'__ge' => OperatorConstant::GE, // Greater than or equal
'__lt' => OperatorConstant::LT, // Less than
'__le' => OperatorConstant::LE, // Less than or equal
'__~' => OperatorConstant::LIKE // Like
];
To query, you can add more params with format:
{param-name}{operator} = {value}
Example:
username = admin---->usernameequaladminname__~ = super---->namelike%super%age__gt = 18---->agegather than18
Full request example
Request to query user with username=admin and name LIKE %super% and age > 18
curl --location --request GET 'http://localhost:8000/api/v1/users?username=admin&name__~=super&age__gt=18'
Validate before Add an entity
Support full Laravel validation: Validation
class UserService extends BaseService
{
public function storeRequestValidate(object $request, array $rules = []): bool|array
{
$rules = [
'username' => 'required|max:255|unique:users',
];
return parent::storeRequestValidate($request, $rules);
}
}
Validate before Update an entity
Support full Laravel validation: Validation
class UserService extends BaseService
{
public function updateRequestValidate(int|string $id, object $request, array $rules = []): bool|array
{
$rules = [
'username' => 'required|max:255|unique:users,id',
];
return parent::updateRequestValidate($id, $request, $rules);
}
}
Service Observe
It supports these observe function:
function postAdd()function postUpdate()function postDelete()function postGet()function postGetAll()
Cache data
If you want to cache data when create update select, implement ShouldCache interface
class UserService extends BaseService implements \YaangVu\LaravelBase\Base\Contract\ShouldCache {}
统计信息
- 总下载量: 11.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 1
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-18