我尝试为我的项目实现自定义服务提供者和外观:
1. 为要包含的类和外观创建文件夹 /app/Helpers/GeoLocation.php /app/Facades/GeoLocation.php
2. 创建类需要提供:Helpers/Geolocation.php
<?php namespace Helpers;
class GeoLocation {
public function test($text) {
return 'Cool ' . $text;
}
}
3. GeoLocation 的 Facade 的创建:Facades/Geolocation.php
<?php namespace Facades;
use Illuminate\Support\Facades\Facade;
class GeoLocation extends Facade {
protected static function getFacadeAccessor() { return 'geolocation'; }
}
4.添加GeoLocationServiceProvider:/Providers/GeoLocationServiceProvider.php
<?php namespace Providers;
use Illuminate\Support\ServiceProvider;
class GeoLocationServiceProvider extends ServiceProvider {
public function register() {
App::bind('geolocation', function()
{
return new \Helpers\GeoLocation();
});
}
}
5.向app.php添加信息
/*
* Application Service Providers...
*/
'App\Providers\AppServiceProvider',
'App\Providers\BusServiceProvider',
'App\Providers\ConfigServiceProvider',
'App\Providers\EventServiceProvider',
'App\Providers\RouteServiceProvider',
'App\Providers\GeoLocationServiceProvider',
6.添加别名
'GeoLocation' => 'Facades\GeoLocation'
之后我执行composer dump-autoload
并使用了新的服务提供者:
路由.php
Route::get('/test', function() {
return GeoLocation::test('test');
});
但它返回以下错误:
FatalErrorException in ProviderRepository.php line 150:
Class 'App\Providers\GeoLocationServiceProvider' not found
in ProviderRepository.php line 150
at FatalErrorException->__construct() in HandleExceptions.php line 131
at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 116
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at ProviderRepository->createProvider() in ProviderRepository.php line 75
at ProviderRepository->load() in Application.php line 446
at Application->registerConfiguredProviders() in RegisterProviders.php line 15
at RegisterProviders->bootstrap() in Application.php line 174
at Application->bootstrapWith() in Kernel.php line 199
at Kernel->bootstrap() in Kernel.php line 110
at Kernel->sendRequestThroughRouter() in Kernel.php line 84
at Kernel->handle() in index.php line 53
at {main}() in index.php line 0
我不知道我错过了什么。是关于文件夹吗?