laravel - 将第三方包设置为自定义服务提供商
问题描述
我正在尝试将一种“存储库模式”应用于自定义服务。
我想做的是将这个库实际绑定到我的自定义服务提供程序以创建一个抽象层,并最终在将来将该库切换到另一个库。
我正在尝试将'providers'
和'aliases'
引用从config/app.php
我的服务提供商移动,但出现Class 'GoogleMaps' not found
错误。
我已经添加App\Providers\GeoServiceProvider::class
到config/app.php
providers 数组中,这是我的相关代码:
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();
}
}
谢谢你,亚历克斯
解决方案
推荐阅读
- android - 如何使用 XML 更改选中的 RadioButton 的 textAppearance?
- ffmpeg - 使用 FFMPEG 的 HLS FLAC 流
- c - 这个单链表程序有什么问题?
- azure - Azure 编排触发器断点未命中
- python - python:如何组织多个相互依赖的类?
- http - 使用什么请求来更新资源而不发送数据
- node.js - node-cron 任务在重新启动服务器后终止
- python - Renaming column in pandas with a multi level index
- reactjs - 这个应用程序中的useObserver 和observer 有什么区别?
- java - 有没有办法在数组中加载多个 fxml,然后在按下按钮而不在 javaFX 中重新加载相同的 fxml 时显示其中一个?