I tried to implement a custom service provider and facade to my project:
1. Creation of folders for class to be included and facade /app/Helpers/GeoLocation.php /app/Facades/GeoLocation.php
2. Creation of class to be provided: Helpers/Geolocation.php
<?php namespace Helpers;
class GeoLocation {
public function test($text) {
return 'Cool ' . $text;
}
}
3. Cration of Facade for GeoLocation: Facades/Geolocation.php
<?php namespace Facades;
use Illuminate\Support\Facades\Facade;
class GeoLocation extends Facade {
protected static function getFacadeAccessor() { return 'geolocation'; }
}
4. Adding 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. Adding information to app.php
/*
* Application Service Providers...
*/
'App\Providers\AppServiceProvider',
'App\Providers\BusServiceProvider',
'App\Providers\ConfigServiceProvider',
'App\Providers\EventServiceProvider',
'App\Providers\RouteServiceProvider',
'App\Providers\GeoLocationServiceProvider',
6. Adding alias
'GeoLocation' => 'Facades\GeoLocation'
After that I executed composer dump-autoload
and used the new service provider:
routes.php
Route::get('/test', function() {
return GeoLocation::test('test');
});
But it returns the following error:
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
I have no clue what I missed out. Is it about the folders?