Forum

IOC

Laravel 5 Service Provider not working

Am getting this error message: Target [App\Repositories\UserRepositoryInterface] is not instantiable.

I created the service provider below in app/providers

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    //protected $defer = true;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //

    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    { 
        $this->app->bind(
            'App\Repositories\UserRepositoryInterface',
            'App\Repositories\UserEloquentRepository'
        );


    }

}

And registered it in config/app

        /*
         * Application Service Providers...
         */
        'App\Providers\AppServiceProvider',
        'App\Providers\BusServiceProvider',
        'App\Providers\ConfigServiceProvider',
        'App\Providers\EventServiceProvider',
        'App\Providers\RouteServiceProvider',

        /*
         * Other Service Providers...
         */
        'App\Providers\RepositoryServiceProvider'


but its not working. Have done composer dump-autoload which i believe is not required, but its still wont work

digitlimit
digitlimit
  • 2 years ago
$this->app->bind('UserRepositoryInterface', function()
{
    return new EloquentUserRepository();
});

Note: I've left away the namespacing so you should add that. Point is the second argument should return an object.

juukie14 said:

$this->app->bind('UserRepositoryInterface', function()
{
   return new EloquentUserRepository();
});

Note: I've left away the namespacing so you should add that. Point is the second argument should return an object.

Still doesn't work. Thanks for responding. I noticed laravel is not even aware of RepositoryServiceProvider.php file which is located in app/providers. laravel is not loading this file. I was able to verified its not being loaded by placing 'exit()' in the register and boot method.

like so:

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    //protected $defer = true;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
exit('just to verify laravel loads this file');
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
           exit('just to verify laravel loads this file');

        $this->app->bind('App\Repositories\UserRepositoryInterface', function()
        {
            return new App\Repositories\UserEloquentRepository();
        });
   }
}

I expect to see 'just to verify laravel loads this file' on page load but instead am getting this error which means laravel is unaware of this file.

BindingResolutionException in Container.php line 785: Target [App\Repositories\UserRepositoryInterface] is not instantiable.

However, when i placed the binding code in App\Providers\AppServiceProvider it works perfectly. I want to create a separate service provider for the repositories instead of clustering App\Providers\AppServiceProvider

This works but not where i wanna place the binding code

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //

    }

    /**
     * Register any application services.
     *
     * This service provider is a great spot to register your various container
     * bindings with the application. As you can see, we are registering our
     * "Registrar" implementation here. You can add your own bindings too!
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            'Illuminate\Contracts\Auth\Registrar',
            'App\Services\Registrar'
        );

        $this->app->bind(
            'App\Repositories\UserRepositoryInterface',
            'App\Repositories\UserEloquentRepository'
        );

    }

}

So my question is , why is laravel not loading my service provider : App\Providers\RespositoryServiceProvider

Do you have this in composer.json?

"psr-4": {
    "App\\": "app/"
}

and is your UserEloquentRepository implementing UserRepositoryInterface?

TorchSK said:

Do you have this in composer.json?

"psr-4": {
   "App\\": "app/"
}

and is your UserEloquentRepository implementing UserRepositoryInterface?

Yeah i do. I finally fixed it.

I simply run composer update and its working now

Thanks.

Composer update works for me.

Same here. some Service Providers are working some aren't. nothing above solution works.

Something I've found that often fixes my problems when working with service providers is to rebuild the "services.json" file. Simply delete the file "services.json" in bootstrap/cache and the file will be re-generated (I think there's an artisan command but this works fine).

  1. composer update didn't worked.
  2. php artisan config:clear didn't worked
  3. php artisan cache:clear didn't worked.

The ServiceProvider just won't execute. Removing AppServiceProvider from config/app.php works but adding another provider doesn't.

I'm using laravel 5.2.

Deleted the provider.php & it's entry from config/app.php

After that created another provider without using "php artisan make:provider" command and it worked like a charm.

Seems like an issue with make:provider.

For others with this problem, the solution is:

composer dumpautoload
php artisan cache:clear