的實例我想換掉我的客戶調用或更好地嘗試圍繞this package做一個包裝,所以我不必每次都寫這個,所以我做了一個新的ServiceProvider,應該叫我Laravel自己的ServiceProvider客戶端調用類型錯誤:參數1傳遞給...必須是
// Create a new client,
// so i dont have to type this in every Method
$client = new ShopwareClient('url', 'user', 'api_key');
對我做的每個請求。
// Later after the Client is called i can make a Request
return $client->getArticleQuery()->findAll();
SwapiServiceProvider
<?php
namespace Chris\Swapi;
use Illuminate\Support\ServiceProvider;
use LeadCommerce\Shopware\SDK\ShopwareClient;
class SwapiServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
}
/**
* Register any package services.
*
* @return void
*/
public function register()
{
$this->app->singleton(ShopwareClient::class, function() {
return new ShopwareClient(
env('SHOPWARE_URL'),
env('SHOPWARE_USER'),
env('SHOPWARE_KEY')
);
});
}
}
我的班級
...
use LeadCommerce\Shopware\SDK\ShopwareClient as Shopware;
class Swapi
{
public function fetchAllArticles(Shopware $shopware)
{
return $shopware->getArticleQuery()->findAll();
}
}
測試
我只是把它在我的routes.php文件來測試
use Chris\Swapi\Swapi;
Route::get('swapi', function() {
// Since this is a package i also made the Facade
return Swapi::fetchAllArticles();
});
,但我得到每次錯誤
FatalThrowableError in Swapi.php line 18: Type error: Argument 1 passed to Chris\Swapi\Swapi::fetchAllArticles() must be an instance of LeadCommerce\Shopware\SDK\ShopwareClient, none given, called in /Users/chris/Desktop/code/swapi/app/Http/routes.php on line 7
,所以我問爲什麼這個
return new ShopwareClient(
env('SHOPWARE_URL'),
env('SHOPWARE_USER'),
env('SHOPWARE_KEY')
);
不叫,每次我打電話的方法e.g $shopware->getArticleQuery()->findAll();
有誰知道爲什麼嗎?
構造函數**被**調用 - 與您的錯誤無關。 **方法**'fetchAllArticles'需要一個'Shopware'的實例作爲參數傳遞,你不這樣做:'return Swapi :: fetchAllArticles(); // < - - 沒有args通過' – Steve
這就是問題,我不需要傳遞任何參數。如果你看看實際的軟件包,客戶似乎不會被調用:https://github.com/LeadCommerceDE/shopware-sdk 那麼我現在該怎麼做? :) – bobbybackblech