Php 拉维尔–;调用API连接
重复调用–假设您需要应用程序与API对话,并且您正在使用guzzle或包装器或其他任何东西。我发现自己必须在每个控制器功能中调用连接,例如:Php 拉维尔–;调用API连接,php,api,laravel,model-view-controller,Php,Api,Laravel,Model View Controller,重复调用–假设您需要应用程序与API对话,并且您正在使用guzzle或包装器或其他任何东西。我发现自己必须在每个控制器功能中调用连接,例如: class ExampleController extends Controller { public function one() { $client = new Client(); $response = $client->get('http://', [ 'query' => [
class ExampleController extends Controller
{
public function one()
{
$client = new Client();
$response = $client->get('http://',
[ 'query' => [ 'secret' => env('SECRET')]]);
$json = json_decode($response->getBody());
$data = $json->object;
// do stuff
}
public function two()
{
$client = new Client();
$response = $client->get('http://',
[ 'query' => [ 'secret' => env('SECRET')]]);
$json = json_decode($response->getBody());
$data = $json->object;
// do stuff
}
}
我该如何更好地处理这件事?我是否使用服务提供商?如果是,我将如何最好地实现这些调用?我是否应该创建另一个控制器并调用每个函数中的所有API连接,然后根据需要包括该控制器并调用每个函数?我应该把它放在一个
\uu结构中吗?
让我们试试依赖倒置原则
好的,一开始听起来可能有点难,我的代码可能有一些打字错误或小错误,但是试试这个
您需要创建接口
namespace app\puttherightnamespace; // this deppends on you
interface ExempleRepositoryInterface
{
public function getquery(); // if you passinga variable -> public function getquery('variable1');
}
现在你必须创建回购协议
class ExempleRepository implements ExempleRepositoryInterface {
public function getquery() {
$client = new Client();
$response = $client->get('http://',
[ 'query' => [ 'secret' => env('SECRET')]]);
$json = json_decode($response->getBody());
return $json->object;
}
现在,最后一步是在服务提供者注册方法中将接口绑定到repo
public function register()
{
$this->app->bind('namespacehere\ExempleRepositoryInterface', 'namespacehere\ExempleRepository');
}
现在,每当你需要一个控制器中的结果时,你所要做的就是取消它
class ExempleController extends Controller {
private $exemple;
public function __construct(ExempleRepositoryInterface $home) {
$this->exemple = $exemple;
}
public function test() {
$data = $this->exemple->getquery(); / you can pass a variable here if you want like this $this->exemple->getquery('variable');
// do stuff
}
这不是最简单的方法,但我想这是最好的方法。让我们试试依赖倒置原则 好的,一开始听起来可能有点难,我的代码可能有一些打字错误或小错误,但是试试这个 您需要创建接口
namespace app\puttherightnamespace; // this deppends on you
interface ExempleRepositoryInterface
{
public function getquery(); // if you passinga variable -> public function getquery('variable1');
}
现在你必须创建回购协议
class ExempleRepository implements ExempleRepositoryInterface {
public function getquery() {
$client = new Client();
$response = $client->get('http://',
[ 'query' => [ 'secret' => env('SECRET')]]);
$json = json_decode($response->getBody());
return $json->object;
}
现在,最后一步是在服务提供者注册方法中将接口绑定到repo
public function register()
{
$this->app->bind('namespacehere\ExempleRepositoryInterface', 'namespacehere\ExempleRepository');
}
现在,每当你需要一个控制器中的结果时,你所要做的就是取消它
class ExempleController extends Controller {
private $exemple;
public function __construct(ExempleRepositoryInterface $home) {
$this->exemple = $exemple;
}
public function test() {
$data = $this->exemple->getquery(); / you can pass a variable here if you want like this $this->exemple->getquery('variable');
// do stuff
}
这不是最简单的方法,但我想这是最好的方法非常感谢!这对我帮助很大。有几个问题,我应该把接口和回购放在laravel目录的什么地方?(in/controllers?)与服务提供商一起,我会将此添加到我的AppServiceProvider文件中吗?否,在app文件夹内(或其外部)创建另一个文件夹,并将repo放在那里,是的,或者您可以将其添加到您的app服务提供商或创建新的服务提供商(顺便说一句,您必须编辑composer.json才能自动加载新文件夹以使其正常工作)非常感谢!这对我帮助很大。有几个问题,我应该将interface和repo放在laravel目录的何处?(在/controllers中?)对于服务提供商,我会将其添加到我的AppServiceProvider文件中吗?不,在AppFolder(或其外部)中创建另一个文件夹并将repo放在那里,是的,或者你可以将其添加到你的AppServiceProvider或创建一个新的服务提供商(顺便说一句,你必须编辑composer.json以自动加载新文件夹以使事情正常进行)