ServiceModel 服務模塊虛類
?? ServiceModel 為抽象類衣陶,無法通過 new 方式實例化闸氮,僅通過 控制器 或其它 模塊 的
getModel
方法獲得教沾,Azalea 中所有服務模塊必須繼承此類
Azalea\ServiceModel 繼承于 Azalea\Model 模塊類
服務路徑定義
服務路徑定義有 3 種方式:
- 默認:在默認情況下,服務路徑將根據(jù) 配置項
['service']['url']
并加上 模塊名 而成或悲,如配置路徑為"http://127.0.0.1:1108"
堪唐,模塊名為"test"
,那么服務路徑為"http://127.0.0.1:1108/test"
男公; - 定義
service
成員屬性:定義該屬性后合陵,服務路徑為配置路徑加上service
屬性值,如
protected $service = 'foo';
// 那么服務路徑將變成 "http://127.0.0.1:1108/foo"
- 定義
serviceUrl
成員屬性:定義該屬性后糠爬,服務路徑將直接使用該值举庶,此方式常用于外部服務,如
protected $serviceUrl = 'http://upyun.com/upload';
當調(diào)用服務方法 (get
, post
, put
, delete
) 時镀琉,如果傳入的第一個參數(shù) $url
為 完整 URL (包含 http://
或 https://
)蕊唐,則直接使用該值;否則钓试,將與 服務路徑 (serviceUrl) 連接組成最終的服務請求 URL
常量
ServiceMode::METHOD_GET
- GET 請求方法
ServiceMode::METHOD_POST
- POST 請求方法
ServiceMode::METHOD_PUT
- PUT 請求方法
ServiceMode::METHOD_DELETE
- DELETE 請求方法
ServiceModel::get
GET 服務請求
mixed ServiceModel::get ( string $url [, array $fields = null] ) throws E500Exception
參數(shù)
$url - 服務路徑
$fields - query 字段數(shù)組副瀑,將使用http_build_query
方法轉(zhuǎn)換成字符串連接到$url
路徑后返回值
調(diào)用結(jié)果異常
拋出 E500Exception 異常范例
$result = $this->get('foo?foo=bar', ['hello' => 'world', 'a' => 1]);
// 將產(chǎn)生類似 foo?foo=bar&hello=world&a=1 這樣的 GET 請求
ServiceModel::post
POST 服務請求
mixed ServiceModel::post ( string $url [, array $fields = null] ) throws E500Exception
參數(shù)
$url - 服務路徑
$fields - form 字段數(shù)組返回值
調(diào)用結(jié)果異常
拋出 E500Exception 異常范例
$result = $this->post('foo?foo=bar', ['hello' => 'world', 'a' => 1]);
ServiceModel::put
PUT 服務請求
同 ServiceModel::post
ServiceModel::delete
DELETE 服務請求
同 ServiceModel::post
ServiceMode::request
請求服務
mixed ServiceModel::request ( int $method, string $url [, array $fields = null [, array $headers = null [, bool $rawContent = false ]]] ) throws E500Exception
參數(shù)
$method - 請求方法 <small>(建議使用類常量)</small>
$url - 服務路徑
$fields - form 字段數(shù)組
$headers - 自定義附加請求頭
$rawContent - 是否返回完整響應內(nèi)容挽鞠,默認只返回"result"
部分返回值
調(diào)用結(jié)果異常
拋出 E500Exception 異常范例
// POST 調(diào)用 foo?foo=bar,傳入 form 數(shù)組和自定義請求頭信认,并按響應原文返回
$result = $this->request(self::METHOD_POST, 'foo?foo=bar', ['hello' => 'world', 'a' => 1], ['user' => 1], true);