mdelariva / api-controller
v1.10.0
2022-10-12 17:10 UTC
Requires
- php: >=7.0
- illuminate/database: >=6
- illuminate/http: >=6
- illuminate/pagination: >=6
- illuminate/support: >=6
- illuminate/validation: >=6
- laravel/framework: >=6
README
Laravel控制器CRUDL特性API服务器。
动作路由
动词 | URI | 动作 | 路由名称 | 描述 |
---|---|---|---|---|
GET | /{实体} | index | {实体}.index | 列表(允许筛选) |
GET | /{实体}/{id} | show | {实体}.show | 显示完整的{实体} |
POST | /{实体} | store | {实体}.store | 创建新的{实体} |
PUT | /{实体}/{id} | update | {实体}.update | 更新{实体} |
DELETE | /{实体}/{id} | destroy | {实体}.destroy | 删除{实体} |
命名规范
- 模型类名称:
Entity
- 控制器类名称:
EntityController
- 资源类名称:
EntityResource
验证
每个模型必须有两个公共静态方法
getValidations()
必须返回一个数组,其中每个元素的键是字段名,值是验证规则(Illuminate\Validation\Validator
)。getValidationsRequired()
必须返回一个必需字段名的数组。
示例
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Example extends Model
{
protected $fillable = [
'name',
'code',
];
// validations
public static function getValidations()
{
$validations = [
'name' => 'string|between:5,255',
'code' => 'string|size:2',
];
return $validations;
}
public static function getValidationsRequired()
{
$validationsRequired = [
'name',
'code',
];
return $validationsRequired;
}
}
索引列表
可用的请求参数
where[] . 有效操作符(附加到字段名后)
- {字段名} => field = value
- {字段名}[] => field IN (value)
- {字段名}_gt => field > value
- {字段名}_ge => field >= value
- {字段名}_lt => field < value
- {字段名}_le => field <= value
- {字段名}_ct => field like "%value%"
- {字段名}_pr => field like "value%"
- {字段名}_ap => field like "%value"
orderBy
- orderByDir
- limit (-1 获取所有记录;默认=20)
属性
你可以在控制器中声明以下属性
名称 | 默认值 | 类型 | 描述 |
---|---|---|---|
limitAmount | 20 | int | 要返回的记录数。 |
indexDefaultOrderBy | string | 排序的字段 | |
indexDefaultOrderByDir | string | 排序方向(asc 或 desc ) | |
model | 使用命名规范生成名称 | string | 模型类的名称 |
modelPath | \App\Models\ | string | 模型类路径 |
resource | 使用命名规范生成名称 | string | 资源类的名称 |
resourcePath | \App\Http\Resources\ | string | 资源类路径 |
钩子
所有钩子类型必须在MdelaRiva\ApiRequests\Libraries\Hook\HookType
类中定义。
动作 | 名称 | 参数 |
---|---|---|
InitBefore | ||
InitAfter | ||
index | IndexBefore | \Illuminate\Http\Request |
index | IndexPreRun | \Illuminate\Database\Eloquent\Builder |
index | IndexAfter | bool , \Illuminate\Database\Eloquent\Model |
store | StoreBefore | \Illuminate\Http\Request |
store | StoreAfter | bool , \Illuminate\Database\Eloquent\Model |
update | UpdateBefore | \Illuminate\Http\Request |
update | UpdateAfter | bool , \Illuminate\Database\Eloquent\Model |
destroy | DeleteBefore | \Illuminate\Database\Eloquent\Model |
destroy | DeleteAfter | int |
使用示例
Laravel
1- 在 routes/api.php
中定义API资源路由
<?php
Route::apiResource( 'articles', 'ArticleController' );
2- 在 app/Models/Article.php
中定义模型验证
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable = [
'name',
'code',
];
public static function getValidations()
{
$validations = [
'name' => 'string|between:5,255',
'code' => 'string|size:2',
];
return $validations;
}
public static function getValidationsRequired()
{
$validationsRequired = [
'name',
'code',
];
return $validationsRequired;
}
}
3- 定义控制器 app/Http/Controllers/ArticleController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use MdelaRiva\ApiRequests\ApiBaseRequests;
use MdelaRiva\ApiRequests\Libraries\Hook\Hook;
use MdelaRiva\ApiRequests\Libraries\Hook\HookType;
use App\Models\ArticleThirdParty;
use App\Http\Resources\ArticleThirdPartyResource;
class ArticleController extends Controller
{
use ApiBaseRequests;
/**
* ApiBaseRequests: Index - Order by
*
* @var string
*/
protected $indexDefaultOrderBy = 'name';
/**
* ApiBaseRequests: Index - Order by direction (ASC, DESC)
*
* @var string
*/
protected $indexDefaultOrderByDir = 'desc';
/**
* ApiBaseRequests: Hooks register
*
* @var string
*/
protected function registerApiHooks(){
Hook::register( HookType::IndexBefore, function(){
$this->model = ArticleThirdParty::class;
$this->resource = ArticleThirdPartyResource::class;
} );
Hook::register( HookType::IndexPreRun, function( $query ){
$query->groupBy( 'party_id' );
} );
}
}
4- 发送请求