anteris-dev / laravel-autotask-client
将 Autotask 客户端集成到 Laravel 中。
Requires
- anteris-dev/autotask-client: ^0.5
- illuminate/support: ^7.0|^8.0
- jenssegers/model: ^1.4
Requires (Dev)
- orchestra/testbench: ^5.5
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-23 00:06:05 UTC
README
此包通过将其注入应用容器来连接 Autotask 客户端和 Laravel。
安装方法
运行 composer require anteris-dev/laravel-autotask-client
。
要发布配置文件,使用命令 php artisan vendor:publish --provider 'Anteris\Autotask\Laravel\ServiceProvider'
。现在您可以在 config/autotask.php 或更偏好地,在 .env 文件中使用以下密钥输入您的 Autotask API 信息。
AUTOTASK_USERNAME=username
AUTOTASK_SECRET=secret
AUTOTASK_INTEGRATION_CODE=integration-code
AUTOTASK_ZONE_URL=https://example.com
为了注册外观以便您可以使用客户端,如 Autotask::tickets()->findById(0)
,请在 Laravel 文件 config/app.php 中的 'aliases' 键中添加以下行。
'Autotask' => Anteris\Autotask\Laravel\Facade::class,
入门指南
您可以将 Autotask 客户端注入,就像其他任何类一样。Laravel 将在需要时自动创建客户端并设置您的凭证。下面是一个示例。
use Anteris\Autotask\Client as AutotaskClient; Route::get('/', function (AutotaskClient $autotask) { $ticket = $autotask->tickets()->findById(0); });
您还可以设置外观(见上方)以更轻松地访问客户端。请参见下面的示例。
Route::get('/', function () { Autotask::tickets()->findById(0); });
有关客户端的更多信息,请参阅此处的文档。
工作进展部分
我们目前正在开发一个可以像其他 Laravel 模型(关系加载等)一样扩展和交互的模型。这些模型支持缓存响应,因此不会不断向 Autotask 服务器发送请求。您可以通过在您的模型上设置 $cache_time
变量来指定响应应缓存多少秒。默认设置为 24 小时,将此设置为 0 将禁用缓存。
- 注意:这些与 Eloquent 模型/关系不兼容。
安装方法
运行 composer require anteris-dev/laravel-autotask-client:dev-master
。
入门指南
通过扩展 Autotask 模型来创建一个新的模型。
use Anteris\Autotask\Laravel\Models\AutotaskModel; class Ticket extends AutotaskModel { protected string $endpoint = 'Tickets'; // Must be the plural form of the endpoint protected int $cache_time = 86400; // 24 hours in seconds } // Supported actions: Ticket::count(); // Used like the count in the query builder Ticket::find(); // Array of IDs or an ID Ticket::get(); // Used like the get in the query builder Ticket::loop(); // Used like the loop in the query builder Ticket::where(); // Used like the where in the query builder Ticket::orWhere(); // Used like the orWhere in the query builder
定义关系
支持当前 Autotask 模型之间的 belongsTo() 和 hasMany() 关系。这些定义如下所示。
use Anteris\Autotask\Laravel\Models\AutotaskModel; class Contact extends AutotaskModel { protected string $endpoint = 'Contacts'; // Must be the plural form of the endpoint protected int $cache_time = 86400; // 24 hours in seconds public function company() { return $this->belongsTo(Company::class); } public function tickets() { return $this->hasMany(Ticket::class); } } // Relationships can be referenced like normal Laravel models: $contact = Contact::find(1); echo $contact->company->companyName;