yaangvu / laravel-base
Laravel API 资源基础
v4.0.42
2024-01-12 05:12 UTC
Requires
- php: ^8.1|^8.2
- ramsey/uuid: ^4.7.1
Requires (Dev)
- illuminate/console: ^7|^8|^9|^10
- illuminate/database: ^7|^8|^9|^10
- illuminate/http: ^7|^8|^9|^10
- illuminate/pagination: ^7|^8|^9|^10
- dev-master
- v4.x-dev
- v4.0.42
- v4.0.41
- v4.0.40
- v4.0.39
- v4.0.38
- v4.0.37
- v4.0.36
- v4.0.35
- v4.0.34
- v4.0.33
- v4.0.32
- v4.0.31
- v4.0.30
- v4.0.29
- v4.0.28
- v4.0.27
- v4.0.26
- v4.0.25
- v4.0.24
- v4.0.23
- v4.0.22
- v4.0.21
- v4.0.20
- v4.0.19
- v4.0.18
- v4.0.17
- v4.0.16
- v4.0.15
- v4.0.14
- v4.0.13
- v4.0.12
- v4.0.11
- v4.0.10
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.x-dev
- v3.0.12
- v3.0.11
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.2.1
- v2.2.0
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.x-dev
- v1.1.46
- v1.1.45
- v1.1.44
- v1.1.43
- v1.1.42
- v1.1.41
- v1.1.40
- v1.1.39
- v1.1.38
- v1.1.37
- v1.1.36
- v1.1.35
- v1.1.34
- v1.1.33
- v1.1.32
- v1.1.31
- v1.1.30
- v1.1.29
- v1.1.28
- v1.1.27
- v1.1.26
- v1.1.25
- v1.1.24
- v1.1.23
- v1.1.22
- v1.1.21
- v1.1.20
- v1.1.19
- v1.1.18
- v1.1.17
- v1.1.16
- v1.1.15
- v1.1.14
- v1.1.13
- v1.1.12
- v1.1.11
- v1.1.10
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.41
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-hoangky
- dev-hungnv1
- dev-namhp
- dev-duyanh
- dev-duynam
- dev-viethqb
- dev-multiple_delete
- dev-tungnd_add_getbycode
- dev-dev-master
This package is auto-updated.
Last update: 2024-09-12 09:06:23 UTC
README
此基础可以帮助创建针对1个特定实体的简单API(CRUD)
安装
composer require yaangvu/laravel-base
对于Laravel
发布配置文件和基础类
php artisan vendor:publish --provider="YaangVu\LaravelBase\Providers\BaseServiceProvider"
对于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
生成命令
如果你想要使用生成命令,请将以下类添加到 providers
数组中的 config/app.php
YaangVu\LaravelBase\Provider\GeneratorServiceProvider::class,
如果你只想在非生产环境中手动加载它,你可以将其添加到 AppServiceProvider
中的 register()
方法
public function register() { if ($this->app->isLocal()) { $this->app->register(\YaangVu\LaravelBase\Provider\GeneratorServiceProvider::class); } // ... }
初始API资源
生成代码
php artisan yaangvu:base Post <option>
选项
- -S: 使用默认Swagger注释生成代码
- -i: 在控制器方法中自动注入服务
生成的代码目录结构
├── app
│ ├── Domains
│ │ └── Post
│ │ ├── Controllers
│ │ │ └── PostController.php
│ │ ├── Models
│ │ │ └── Post.php
│ │ └── Services
│ │ └── PostService.php
路由
Route::base('/posts', \App\Domains\Post\Controllers\PostController::class);
用法
动态查询参数
支持的运算符
$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
];
查询时,可以添加更多格式为的参数
{param-name}{operator} = {value}
示例
username = admin
---->username
等于admin
name__~ = super
---->name
类似于%super%
age__gt = 18
---->age
大于18
完整的请求示例
请求查询用户,其中 username=admin
和 name LIKE %super%
以及 age > 18
curl --location --request GET 'https://:8000/api/v1/users?username=admin&name__~=super&age__gt=18'
在添加实体之前进行验证
支持完整的Laravel验证:[验证](https://laravel.net.cn/docs/master/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);
}
}
在更新实体之前进行验证
支持完整的Laravel验证:[验证](https://laravel.net.cn/docs/master/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);
}
}
服务观察
它支持以下观察函数
function postAdd()
function postUpdate()
function postDelete()
function postGet()
function postGetAll()
缓存数据
如果你想在 create
、update
、select
时缓存数据,则实现 ShouldCache
接口
class UserService extends BaseService implements \YaangVu\LaravelBase\Base\Contract\ShouldCache {}