IoC not being resolved automatically

Multi tool use
Multi tool use


IoC not being resolved automatically



I'm implementing repositories in my app. In my PresupuestoController (Presupuesto = Estimate in spanish) PresupuestoRepo gets injected in the constructor:


<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsPaciente;
use AppRepositoriesPresupuestoRepository;

class PresupuestosController extends Controller
{
protected $presupuestoRepo;

public function __construct(PresupuestoRepository $presupuestoRepo )
{
$this->presupuestoRepo = $presupuestoRepo;
}

/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create(Paciente $paciente)
{
$presupuesto = $this->presupuestoRepo->createNew($paciente);
dd($presupuesto);
return view('presupuesto.edit', compact('paciente'));
}
}



So far so god. It works. But now I need to inject a dependency in the PresupestoRepo itself, PacienteRepository (Paciente = Patient in spanish)


<?php

namespace AppRepositories;

use CarbonCarbon;
use AppModelsPresupuesto;
use AppRepositoriesPacienteRepository;

class PresupuestoRepository extends BaseRepository {

protected $pacienteRepo;

public function __constructor(PacienteRepository $pacienteRepo)
{
// THIS CONSTRUCTOR IS **NEVER** CALLED !!!

$this->pacienteRepo = $pacienteRepo;
dd($pacienteRepo); // here is the problem: pacienteRepo is NULL !!!
}

public function getRepo()
{
return $this->presupuestoRepo;
}
}



but PaceinteRepository is not automaticly invoqued, getting null instead.



For the sake of completeness , PacienteRepository:


<?php

namespace AppRepositories;

use AppModelsPaciente;
use AppModelsOdontograma;



class PacienteRepository extends BaseRepository {

public function __constructor(){}

/**
* @return string
*/
public function getClassName()
{
return 'AppModelsPaciente';
}
}



How to get a PacienteRepo injected in PresupuestoRepository's constructor?





Have you added your repository in AppServiceProviders.php?
– Gaurav Dave
Jan 30 at 12:10





@GauravDave Is it required? PresupuestoRepository is not registered and is working, being injected in PresupuestoController's constructor. What makes the difference? Sorry, i'm new to this and may be i'm missing sth.
– Carlos Mora
Jan 30 at 12:20






It is preferred, as it binds the interface with its implementation.
– Gaurav Dave
Jan 30 at 12:24





Repository is not a service provider, so it cannot be registered. My doubt is I'm not able to see the difference. PresupuestoRepository doen't need registration to be injected in the controller...
– Carlos Mora
Jan 30 at 12:31





Shame on me! __constructor instead of __construct. That was the problem,
– Carlos Mora
Jan 30 at 17:56









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

gN2 GcR7c9,X2,SwIH,6NZ4aeh5F1h9fS8,5YFX U6Vwjsp5bNS39,FAW hxb2BYbs1Z,N4Lwogptrm
C nWDwPKNN BtKUSxHCpZOMu QK TYY5 bxYGVGi3 0EG4N8bw,C3a6nlV5UfzMU5M wbY9L1ODjqBuZe6r8b0c,9w NCVaSHSLr4c

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask