Back

Laravel 5 Service Provider not working


digitlimit posted 2 years ago

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

juukie replied 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.

digitlimit replied 2 years ago

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

TorchSK replied 2 years ago

Do you have this in composer.json?

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

and is your UserEloquentRepository implementing UserRepositoryInterface?

digitlimit replied 2 years ago

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

viratgaywala replied 2 years ago

Thanks.

Composer update works for me.

oshanz replied 2 years ago

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

john052 replied 2 years ago

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).

nitinsurana replied 1 year ago
  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.

nitinsurana replied 1 year ago

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.

AlexanderWright replied 8 months ago

For others with this problem, the solution is:

composer dumpautoload
php artisan cache:clear
Hasan Baidoun replied 2 weeks ago

In such case, the solution for me was to reorder the 'providers' in my config/app.php . I put my service provider class name at the top of application defaults, so it worked.

/*
         * Application Service Providers...
         */
        App\Providers\MyServiceProvider::class,
        
        App\Providers\ComposerServiceProvider::class,
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us