daniesy / rodels
此包最新版本(1.0.13)没有可用的许可证信息。
Laravel的远程模型
1.0.13
2023-09-09 21:55 UTC
README
Laravel的远程模型。
入门指南
按照以下说明在您的Laravel项目中安装并运行Rodels。
先决条件
您需要在您的机器上安装 Composer,但由于您已经在使用Laravel,我猜您可以跳过这一步。
安装
首先,通过Composer安装此包。
composer require daniesy/rodels
Laravel用户
对于Laravel用户,有一个服务提供者您可以用来自动注册必要的绑定。
Laravel 5.5+ 用户:此步骤可以跳过,因为我们可以在框架中自动注册包。
// config/app.php 'providers' => [ '...', Daniesy\Rodels\RodelsServiceProvider::class ], 'aliases' => [ '...', 'Remote' => \Daniesy\Rodels\Facade\Remote::class, ],
当此提供者启动时,您将能够访问一个有用的 Remote
门面,您可以在控制器中使用它。
public function index() { $users = Remote::users()->list(); return response()->json($users); }
在Laravel 5中,当然需要在控制器顶部添加
use Remote;
。
默认设置
如果使用Laravel,您只需关注两个配置选项。首先,发布默认配置
php artisan vendor:publish
// Or...
php artisan vendor:publish --provider="Daniesy\Rodels\RodelsServiceProvider"
这将向: config/rodels.php
添加一个新的配置文件。
<?php return [ /* |-------------------------------------------------------------------------- | The remote API host |-------------------------------------------------------------------------- | | Set this value to the url of the remote API you want to connect to | */ 'host' => env('RODELS_HOST'), /* |-------------------------------------------------------------------------- | The HTTP client used to send HTTP requests |-------------------------------------------------------------------------- | | This option defines the http client that rodels will be using to connect | to the remote API. | | Supported: "curl" */ 'client' => 'curl', /* |-------------------------------------------------------------------------- | The authentication method |-------------------------------------------------------------------------- | | You can set the authentication method that will be used when connecting | to the API. | | Supported: "key" */ 'auth' => 'key', /* |-------------------------------------------------------------------------- | Authentication configuration |-------------------------------------------------------------------------- | | Configure the key authentication method. | */ 'key' => [ 'name' => 'api-key', 'value' => env('RODELS_KEY') ], ];
使用库。
我们添加了两个 artisan 命令,一个用于创建端点,另一个用于 rodels。
端点
使用Rodels的第一步是创建一个端点。
php artisan make:endpoint Users -r
这将创建一个新类在 app\Endpoints
中 - 在这种情况下 Users.php
。在这个类中,您应该定义与您的端点相关的所有方法。
所有端点名称应该是复数名词形式
使用 -r
或 --rodel
选项,将自动为该端点创建一个 rodel。
<?php namespace App\Endpoints; use App\Rodels\User; use Daniesy\Rodels\Api\Components\Endpoint; use Daniesy\Rodels\Api\Components\RodelCollection; use Daniesy\Rodels\Api\Exceptions\InvalidModelException; class Users extends Endpoint { /** * @param array $params * @return RodelCollection * @throws InvalidModelException */ public function list(array $params = []) : RodelCollection { $response = $this->authRequest()->get("users", $params); return new RodelCollection($response, User::class); } /** * @param string $name * @return User */ public function find(string $name) : User { $response = $this->authRequest()->get("users", compact('name')); return new User($response); } }
Rodels
对于每个端点,都应该创建一个 rodel。Rodels 类似于模型,但不是用于数据库,而是用于远程API。
使用此命令创建一个新的 rodel
php artisan make:rodel User
这将创建一个新类在 app\Rodels
中 - 在这种情况下 User.php
。
所有 rodels 名称应该是单数名词形式。