sanchescom / laravel-rest
此库提供用于使用模型和集合与REST API一起工作的工具和接口。
0.1.2
2019-08-26 10:58 UTC
Requires
- php: ^7.2
- guzzlehttp/guzzle: ^6.3
- jenssegers/model: ^1.2
- laravel/framework: 5.8.*
- sanchescom/json-helper: ^0.1
Requires (Dev)
- donatj/mock-webserver: ^2.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-09-28 20:09:22 UTC
README
此库提供用于使用REST API以及使用Laravel模型和集合的工具和接口。
安装
使用Composer在项目的根目录中要求此包。 Composer
$ composer require sanchescom/laravel-rest
Laravel
更新composer后,将ServiceProvider添加到config/app.php中的提供者数组中。
'providers' => [ ... Sanchescom\Rest\RestServiceProvider::class, ],
Lumen
更新composer后,将以下行添加到bootstrap/app.php以注册提供者。
$app->register(Sanchescom\Rest\RestServiceProvider::class);
配置
在config/rest.php中更改默认的rest API名称。
'default' => env('REST_CLIENT', 'localhost'),
并添加新的API配置
<?php return [ 'clients' => [ 'localhost' => [ 'provider' => 'guzzle', 'base_uri' => 'https:///', 'options' => [ 'headers' => [ 'Content-Type' => 'application/json', ], ], ], ], ];
模型
此包包含一个启用了REST功能的模型类,您可以使用它来定义对应集合的模型。
<?php use Sanchescom\Rest\Model; class User extends Model { /** {@internal} */ protected $dataKey = 'data'; /** {@internal} */ protected $fillable = [ "id", "first_name", "last_name", "email", ]; }
示例
URL : /api/users
内容示例
对于用户。
{
"data": [
{
"id": 1,
"first_name": "Joe",
"last_name": "Bloggs",
"email": "joe25@example.com"
},
{
"id": 2,
"first_name": "Bob",
"last_name": "Jonson",
"email": "bob25@example.com"
}
]
}
基本用法
获取所有模型
$users = User::get();
通过ID获取记录
$user = User::get('1');
通过IDa获取记录
$users = User::getMany(['1', '2']);
Wheres
$users = User::get()->where('first_name', 'Bob');
更多信息请查看 https://laravel.net.cn/docs/collections
插入、更新和删除
保存新模型
User::post(['first_name' => 'Tim']);
更新模型
要更新模型,您可以检索它,更改一个属性,然后使用put方法。
$user = User::get('2'); $user->email = 'john@foo.com'; $user->put();
或者通过其键更新模型
User::put('2', ['email' => 'john@foo.com']);
删除模型
要删除模型,只需在实例上调用delete方法即可。
$user = User::get('1'); $user->delete();
或者通过其键删除模型
User::delete('1');
贡献
请阅读 CONTRIBUTING.md 了解我们的行为准则和向我们提交pull请求的过程。
版本控制
我们使用 SemVer 进行版本控制。有关可用的版本,请参阅此存储库上的 标签 。
作者
- Efimov Aleksandr - 初始工作 - Sanchescom
还可以查看参与此项目的 贡献者列表 。
许可证
此项目采用MIT许可证 - 有关详细信息,请参阅LICENSE.md文件。