0

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?

sesc360
  • 3,155
  • 10
  • 44
  • 86
  • please attach your GeoLocationServiceProvider as well – terry low Mar 26 '15 at 11:29
  • I just edited the question and added the provider – sesc360 Mar 26 '15 at 11:33
  • try 'Providers\GeoLocationServiceProvider', because your service providers is not under App namespace – terry low Mar 26 '15 at 11:34
  • I adapted the path in the app.php to 'Providers\GeoLocationServiceProvider' but now the error is `Class 'Providers\GeoLocationServiceProvider' not found` – sesc360 Mar 26 '15 at 11:36
  • did you add the GeoLocationServiceProvider to you composer autoload ? – terry low Mar 26 '15 at 11:39
  • When I run `composer update` the following error comes up: `Error Output: PHP Fatal error: Class 'Providers\GeoLocationServiceProvider' not found in /home/vagrant/mfserver/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 150` – sesc360 Mar 26 '15 at 11:41
  • No. How would I need to add this? This is the content right now `"autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/" } },` – sesc360 Mar 26 '15 at 11:42
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73843/discussion-between-terry-low-and-isarfaktor). – terry low Mar 26 '15 at 11:50
  • Thank you Terry... The missing link was as you said in the Service Provider register function. I need to use \App::bind and not App::bind – sesc360 Mar 26 '15 at 12:05

0 Answers0