$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
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).
composer update
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
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!
The Laravel portal for problem solving, knowledge sharing and community building.
The community