Target is not instantiable. Laravel 5 - App binding service provider - php

I'm getting this error:
BindingResolutionException in compiled.php line 1029:
Target [App\Models\Contracts\Repositories\IUserRepository] is not instantiable.
My code is as follows:
Interface:
namespace App\Models\Contracts\Repositories;
use App\Models\Objects\DTO\User;
interface IUserRepository
{
function Create( User $user );
}
Concrete:
namespace App\Models\Concrete\Eloquent;
use App\Models\Contracts\Repositories\IUserRepository;
use App\Models\Objects\DTO\User;
class EqUserRepository implements IUserRepository
{
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
public function Create( User $user )
{
return User::create( [
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'username' => $user->username,
'email' => $user->email,
'password' => bcrypt( $user->password ),
] );
}
}
Service Provider:
<?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(
'App\Models\Contracts\Repositories\IUserRepository',
'App\Models\Concrete\Eloquent\EqUserRepository'
);
}
}
Controller:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\Models\Contracts\Repositories\IUserRepository;
use App\Models\Objects\DTO\User;
class AuthController extends Controller
{
protected $auth;
private $userRepository;
public function __Construct(
Guard $auth,
IUserRepository $userRepo )
{
...
Folder structure
I have also seen that I may need to declare the namespaces in my composer.json, So i have tried the following as well as just the above:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\Concrete\\Eloquent\\": "app/Models/Concrete/Eloquent/",
"App\\Models\\Contracts\\Repositories\\": "app/Models/Contracts/Repositories/",
"App\\Models\\Objects\\DTO\\": "app/Models/Objects/DTO/"
}
},
and then ran composer dump-autoload
Any ideas what I am forgetting to do?

I noticed the compiled.php was not being updated.
Run this function in cmd line on the root folder of your project:
php artisan clear-compiled

If you also run in below error :
BindingResolutionException in Container.php line 749: Target
[App\Contracts\TestContract] is not instantiable.
Clear your config cache with :
php artisan config:clear

I had same error solved by adding repository in app/providers/AppServiceProvider in register method like the below.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->register(RepositoryServiceProvider::class); // here
}
}

Related

Target [Interface] is not instantiable while building [Controller]

I'm working with Laravel 9 and I have a Controller like this:
use App\Repositories\HomeRepositoryInterface;
class HomeController extends Controller
{
private $homeRespository;
public function __construct(HomeRepositoryInterface $homeRepository)
{
$this->homeRespository = $homeRepository;
}
...
And here is the HomeRepositoryInterface:
<?php
namespace App\Repositories;
interface HomeRepositoryInterface
{
public function newest();
}
And this is the HomeRepository itself:
<?php
namespace App\Repositories;
use App\Models\Question;
class HomeRepository implements HomeRepositoryInterface
{
public function newest()
{
return $ques = Question::orderBy('created_at', 'DESC')->paginate(10);
}
}
But now I get this error:
Target [App\Repositories\HomeRepositoryInterface] is not instantiable while building [App\Http\Controllers\HomeController].
So what's going wrong here?
How can I solve this issue?
It seems that you did not introduce the service container.
For this, it is better to create a service provider as shown below and introduce the repository class to the container.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Repositories\HomeRepositoryInterface;
use App\Repositories\HomeRepository;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
// Bind Interface and Repository class together
$this->app->bind(HomeRepositoryInterface::class, HomeRepository::class);
}
}
Next, you should introduce this service provider in the config/app.php file.
'providers' => [
...
...
...
App\Providers\RepositoryServiceProvider::class,
],

Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\ServiceProvider - Laravel

i have this error when I have controller using RedisProvider. im already set RedisProvider in app.php. what i'm misssing to make RedisProvider works? also this is just an example, the actual code even not using Redis, im trying to understand how service container and service provider works
app/Http/Controllers/MyController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Providers\RedisProvider;
class MyController extends Controller
{
//
public function Index(RedisProvider $redis) {
$redis->set("TEST","AKARAPACI 2550505");
}
}
app/Providers/RedisProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Redis\RedisLib;
class RedisProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->singleton(RedisLib::class, function ($app) {
return new RedisLib();
});
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
app/Redis/RedisLib
<?php
namespace App\Redis;
use Redis;
class RedisLib {
public $redis;
public function __construct() {
$this->redis = new redis();
$this->redisConnectionStatus = $this->redis->pconnect("192.168.7.147", 6379);
if($this->redisConnectionStatus) {
echo "REDIS_CONNECTED\n";
} else {
echo "ERR_CONNECT";
}
}
}
/config/app.php (only portion)
'providers' => [
//<...existing provider not included here-->
App\Providers\RedisProvider::class,
],
error Unresolvable dependency resolving [Parameter #0 [ $app ]] in class Illuminate\Support\ServiceProvider is occured on line 52 of index.php, im using Laravel 9
...
51 $response = $kernel->handle(
52 $request = Request::capture()
53 )->send();
...

Laravel 5.7: target is not instantiable while building

I know there are so many answer, but I cannot really solve this.
I did follow this answer (How to make a REST API first web application in Laravel) to create a Repository/Gateway Pattern on Laravel 5.7
I have also the "project" on github, if someone really kindly want test/clone/see : https://github.com/sineverba/domotic-panel/tree/development (development branch)
App\Interfaces\LanInterface
<?php
/**
* Interface for LAN models operation.
*/
namespace App\Interfaces;
interface LanInterface
{
public function getAll();
}
App\Providers\ServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
/**
* Solve the "Key too long" issue
*
* #see https://laravel-news.com/laravel-5-4-key-too-long-error
*/
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->register(RepositoryServiceProvider::class);
}
}
App\Providers\RepositoryServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(
'app\Interfaces\LanInterface', // Interface
'app\Repositories\LanRepository' // Eloquent
);
}
}
App\Gateways\LanGateway
<?php
/**
* The gateway talks with Repository
*/
namespace App\Gateways;
use App\Interfaces\LanInterface;
class LanGateway
{
protected $lan_interface;
public function __construct(LanInterface $lan_interface) {
$this->lan_interface = $lan_interface;
}
public function getAll()
{
return $this->lan_interface->getAll();
}
}
App\Repositories\LanRepository
<?php
/**
* Repository for LAN object.
* PRG paradigma, instead of "User"-like class Model
*/
namespace App\Repositories;
use App\Interfaces\LanInterface;
use Illuminate\Database\Eloquent\Model;
class LanRepository extends Model implements LanInterface
{
protected $table = "lans";
public function getAll()
{
return 'bla';
}
}
I did add also App\Providers\RepositoryServiceProvider::class, in providers section of config\app.php
This is finally the controller (I know that it is not complete):
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Gateways\LanGateway;
class LanController extends Controller
{
private $lan_gateway;
/**
* Use the middleware
*
* #return void
*/
public function __construct(LanGateway $lan_gateway)
{
$this->middleware('auth');
$this->lan_gateway = $lan_gateway;
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$this->lan_gateway->getAll();
return view('v110.pages.lan');
}
}
And the error that I get is
Target [App\Interfaces\LanInterface] is not instantiable while building [App\Http\Controllers\LanController, App\Gateways\LanGateway].
I did try:
php artisan config:clear
php artisan clear-compiled
I think #nakov might be right about it being case-sensitive. I don't believe PHP itself cares about upper/lowercase namespaces, but the composer autoloader and the Laravel container use key->value array keys, which do have case-sensitive keys, to bind and retrieve classes from the container.
To ensure the names always match, try using the special ::class constant instead, like this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Repositories\LanRepository;
use App\Interfaces\LanInterface;
class RepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(
LanInterface::class,
LanRepository::class
);
}
}
In my case i forgot to enlist the provider to confit/app.php that's why the error.
Clear the old boostrap/cache/compiled.php:
php artisan clear-compiled
Recreate boostrap/cache/compiled.php:
php artisan optimize

View Composer not working Laravel

I have App\Http\NotificationComposer.php:
namespace App\Http\ViewComposers;
use Illuminate\View\View;
class NotificationComposer
{
public $notifications;
public function __construct(){
$this->notifications = json_decode(\Auth::user()->notifications, true);
}
public function compose (View $view)
{
dd($this->notifications);
$view->with('notifications');
}
}
I also have a App\Providers\ComposerServiceProvider.php:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
view()->composer(
'app',
'App\Http\ViewComposers\NotificationComposer'
);
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
And in my config\app.php:
'providers' => [
...
App\Providers\ComposerServiceProvider::class,
...
],
];
I believe I have set up the view composer correctly, however everytime layouts\app.blade.php is loaded (the default bootstrap bar in laravel) it isn't rendering any notifications, even though there are some in the database, I have attempted to dd them as you can see in the view composer.
Does anyone have any ideas why this might be happening or what I haven't done properly?
Thanks,
Ok I've made a workaround instead. This wasn't registering for some reason so I place this inside of the AppServiceProivder.php
view()->composer(
'layouts.app', function ($view){
$view->with('notifications', json_decode(\Auth::user()->notifications, true));
});
It's not as clean but for something this small, I guess it's ok.

Laravel 5 Custom ServiceProvider not found

I'm making a Laravel ServiceProvider for a package.
The package is https://github.com/sumocoders/Teamleader
I get the following error
FatalErrorException in ProviderRepository.php line 150:
Class 'Notflip\Teamleader\TeamleaderServiceProvider' not found
I have no clue what I'm doing wrong, Here's my folder structure
composer.json in my package
"autoload": {
"psr-4": {
"Notflip\\Teamleader": "src/"
}
}
TeamleaderServiceProvider
<?php namespace Teamleader\Laravel;
use Illuminate\Support\ServiceProvider;
class TeamleaderServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* #return void
*/
public function publishes()
{
$this->publishes([
__DIR__.'/Config/config.php' => config_path('teamleader.php'),
]);
}
public function register()
{
$this->app->bind('Teamleader\Laravel', function () {
return new Teamleader(config('teamleader.API_GROUP'), config('teamleader.API_SECRET'), config('teamleader.SSL'));
});
}
}
Facade
<?php namespace Teamleader\Laravel\Facade;
class Teamleader extends Facade
{
protected static function getFacadeAccessor()
{
return 'Teamleader';
}
}
In my config.php I added the following line to the providers
'Notflip\Teamleader\TeamleaderServiceProvider',
And this line to the aliasses
'Teamleader'=> 'Notflip\Teamleader\Facade\Teamleader'
Anyone has any idea what I might be doing wrong? Thank you! I'm so close to the result!
Your definition in composer is missing the initial slashes and you haven't specified the path to src from root.
"psr-4": {
"\\Notflip\\Teamleader": "notflip/teamleader-laravel/src/"
}
Also your declaration of the name space at the top of TeamleaderServiceProvider is wrong, it should be:
<?php namespace Notflip\Teamleader;
Solved
In the facade, the IOC binding was named wrong ( wrong case )
The name should have been 'teamleader' in lowercase.
Facade
class Teamleader extends Facade
{
protected static function getFacadeAccessor()
{
return 'teamleader';
}
}
Service Provider
<?php namespace Teamleader\Laravel;
use Illuminate\Support\ServiceProvider;
class TeamleaderServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* #return void
*/
public function publishes()
{
$this->publishes([
__DIR__.'/Config/config.php' => config_path('teamleader.php'),
]);
}
public function register()
{
$this->app->bind('teamleader', function () {
return new Teamleader(config('teamleader.API_GROUP'), config('teamleader.API_SECRET'), config('teamleader.SSL'));
});
}
}

Categories