Support the ongoing development of Laravel.io →
posted 9 years ago
IOC
Last updated 1 year ago.
0
$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.

Last updated 9 years ago.
0

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

Last updated 9 years ago.
0

Do you have this in composer.json?

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

and is your UserEloquentRepository implementing UserRepositoryInterface?

Last updated 9 years ago.
0

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

0

Thanks.

Composer update works for me.

0

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

Last updated 8 years ago.
0

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

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

0

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.

0

For others with this problem, the solution is:

composer dumpautoload
php artisan cache:clear
Last updated 7 years ago.
0

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,
Last updated 6 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.