我正在尝试对自定义服务应用某种“存储库模式”。
我想做的是实际上将this库绑定到我的自定义服务提供商以创建一个抽象层,并最终在将来将该库与另一个库交换。
我正在尝试将'providers'
和'aliases'
引用从config/app.php
移到我的服务提供商,但是出现Class 'GoogleMaps' not found
错误。
我已将App\Providers\GeoServiceProvider::class
添加到config/app.php
提供者数组中,这是我的相关代码:
GeoServiceProvider.php
(我的自定义服务提供商)
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class GeoServiceProvider extends ServiceProvider
{
/** * Register services.
*
* @return void
*/
public function register()
{
$this->app->bind(\App\Interfaces\GeoInterface::class, \App\Services\GoogleGeoService::class);
$this->app->alias(\GoogleMaps\Facade\GoogleMapsFacade::class, 'GoogleMaps');
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
}
}
GeoInterface.php
定义标准方法的界面
<?php
namespace App\Interfaces;
interface GeoInterface
{
public function geoCode();
}
GoogleGeoService.php
(实际的库实现)
<?php
namespace App\Services;
use App\Interfaces\GeoInterface;
class GoogleGeoService implements GeoInterface
{
public function geoCode()
{
$response = \GoogleMaps::load( 'geocoding' ) <--- HERE IS WHERE I GET THE ERROR
->setParamByKey( 'latlng', "45.41760620,11.90208370")
->setEndpoint( 'json' )
->get();
$response = json_decode($response, true);
return $response;
}
}
TestController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Interfaces\GeoInterface;
class TestController extends Controller
{
protected $geoService;
public function __construct(GeoInterface $geoService) {
$this->geoService = $geoService;
}
public function index() {
return $this->geoService->geoCode();
}
}
谢谢你, 亚历克斯