Php 使用RESTAPI的最佳设计模式是什么
我想在Laravel(一个MVC框架)中使用RESTAPI,但我求助于使用Php 使用RESTAPI的最佳设计模式是什么,php,laravel,design-patterns,repository-pattern,Php,Laravel,Design Patterns,Repository Pattern,我想在Laravel(一个MVC框架)中使用RESTAPI,但我求助于使用\uu调用,我想知道是否有更好的设计模式 我知道这是一个错误的选择,我正在寻找另一种模式,但这是我的存储库类: namespace App\Repositories; use App\Models\OnlinePayment; use App\Models\Order; use App\Models\Transaction; use App\Models\User; use GuzzleHttp\Client; use
\uu调用
,我想知道是否有更好的设计模式
我知道这是一个错误的选择,我正在寻找另一种模式,但这是我的存储库类:
namespace App\Repositories;
use App\Models\OnlinePayment;
use App\Models\Order;
use App\Models\Transaction;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use SoapClient;
class Bank
{
protected $http;
protected $user;
public function __construct()
{
$this->http = new Client;
}
protected function index()
{
$url = config('Bank.url') . '/v2/quantum/users/' . $this->user->national_id . '/report';
$data = [
'user_gender' => $this->user->gender ?? 1,
'user_name' => $this->user->name,
'user_family' => $this->user->family ?? 'خالی',
'user_mobile' => $this->user->mobile,
'user_type' => $this->user->type->name,
];
$options = $this->options($data);
$res = $this->http->request('GET', $url, $options);
$response = json_decode($res->getBody(), true);
return $response;
}
protected function indexData($request)
{
$url = config('Bank.url') . '/v2/quantum/users/' . $this->user->national_id . '/customers';
$options = $this->options($request->all());
$res = $this->http->request('GET', $url, $options);
$response = response()->json(json_decode($res->getBody(), true), $res->getStatusCode());
return $response;
}
protected function show($national_id)
{
$url = config('Bank.url') . '/v2/quantum/users/' . $this->user->national_id . '/customers/' . $national_id;
$options = $this->options([]);
$res = $this->http->request('GET', $url, $options);
if ($res->getStatusCode() == 404) {
abort(404);
}
$response = json_decode($res->getBody(), true);
return $response;
}
protected function store($request)
{
$http = new Client;
$url = config('Bank.url') . '/v2/quantum/users/' . $this->user->national_id . '/customers';
$this->user = auth()->user();
$data = array_merge(
[
'customer_national_id' => $request->national_id,
'customer_gender' => $request->gender,
'customer_name' => $request->name,
'customer_family' => $request->family,
'customer_phone' => $request->phone,
'customer_mobile' => $request->mobile,
'customer_city_id' => $request->city_id,
], [
'user_name' => $this->user->nanfig() is a hidden dependency. The settings should also be passed via the construcme,
'user_family' => $this->user->family ?? 'خالی',
'user_mobile' => $this->user->mobile,
'user_type' => $this->user->type->name,
'user_gender' => $this->user->gender ?? 1,
]
);
$res = $http->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . config('Bank.token'),
],
'json' => $data,
'http_errors' => false
]);
if (! in_array($res->getStatusCode(), [200, 422])) {
$error = ValidationException::withMessages([
'name' => 'خطای ' . $res->getStatusCode() . ' در تعویض کالا'
]);
throw $error;
}
$response = response()->json(json_decode($res->getBody(), true), $res->getStatusCode());
return $response;
}
protected function options($data)
{
$options = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . config('Bank.token'),
],
'json' => $data,
'http_errors' => false
];
return $options;
}
public function __call($method, $arguments) {
if (method_exists($this, $method)) {
if (! isset($arguments[0]) || ! $arguments[0] instanceof User) {
$this->user = auth()->user();
} else {
$this->user = $arguments[0];
unset($arguments[0]);
}
return call_user_func_array(array($this, $method), $arguments);
}
}
}
然后在控制器构造函数中创建它的实例:
public function __construct()
{
$this->Bank = new Bank();
}
并在控制器中使用它,如下所示:
$response = $this->Bank->indexData($user, $request);
或者这个:
$response = $this->Bank->indexData($request);
我认为显示的类不是Repository类,因为Repository只负责从数据源读取和写入日期。您的类做得太多,违反了所有基本的MVC原则 有些人认为我会解决:
- 存储库不负责创建响应视图数据(如JSON)
- 存储库不负责创建响应对象
- 存储库独立于请求/响应
- 方法名
毫无意义,因为存储库不是控制器操作。不要将模型层与控制器层混合使用index
- config()是一个隐藏的依赖项。设置也应该通过构造函数传递
- 创建一个类
BankApiClient
- 不要使用像
\u call
- 而是使用公共方法,如:getUserByNationalId(int$nationalId):UserData
- 等等
- 让控制器操作使用BankApicClient的结果创建/呈现json响应
- 存储库不负责创建响应视图数据(如JSON)
- 存储库不负责创建响应对象
- 存储库独立于请求/响应
- 方法名
毫无意义,因为存储库不是控制器操作。不要将模型层与控制器层混合使用index
- config()是一个隐藏的依赖项。设置也应该通过构造函数传递
- 创建一个类
BankApiClient
- 不要使用像
\u call
- 而是使用公共方法,如:getUserByNationalId(int$nationalId):UserData
- 等等
- 让控制器操作使用BankApicClient的结果创建/呈现json响应
- \uu调用是php的一个神奇方法,它允许在对象实例之外执行受保护的方法,这是类可见性的破裂
如果要从外部调用方法,则该方法必须是公共的
public function __construct()
{
$this->bank = new Bank()
}
使用依赖项的自动注入
public function __construct(Bank $bank)
{
$this->bank = $bank;
}
__调用是php的一个神奇方法,它允许在对象实例之外执行受保护的方法,这是类可见性的破裂 如果要从外部调用方法,则该方法必须是公共的
public function __construct()
{
$this->bank = new Bank()
}
使用依赖项的自动注入
public function __construct(Bank $bank)
{
$this->bank = $bank;
}
谢谢,我有点困惑,你的意思是我只需要纠正这些点?你能告诉我应该使用的设计模式的名称吗?我会说这是一个MVC和SOA(面向服务的体系结构)体系结构,它尊重可靠的原则,尽可能对TDD友好。谢谢,我有点困惑,你的意思是我只需要纠正这些点?你能告诉我应该使用的设计模式的名称吗?我会说它是一个MVC和SOA(面向服务的体系结构)体系结构,它尊重可靠的原则,尽可能对TDD友好。