0
I have a class that will manage some of my dependencies. I want her to be called before any method be called on the controller, and for that I am using Hooks. The problem is that I don’t know how to call this object that was once instantiated in hook in my methods of controller. How could I do that? Look at you:
In the hook i am using this setting:
$hook['post_controller_constructor'][] = array(
'class' => 'DependencyInjection',
'function' => 'initContainer',
'filename' => 'DependencyInjection.php',
'filepath' => 'hooks');
And the file dependencia.php
is like this:
<?php
use Pimple\Container;
$container = new Container();
$container['guzz'] = function($c) {
return new GuzzleHttp\Client();
};
The hook was created that way:
<?php
class DependencyInjection {
public $container;
public function initContainer () {
return $this->container = require_once '/var/www/projetoSize/dependencia.php';
}
}
I want to wear this on mine controller thus:
class Teste extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
var_dump($this->guzz);
}
}
But it is giving error with the following message:
Message: Undefined Property: Test::$Guzz
You are trying to load a library, and that is not the right way to do it. Read Creating Libraries. If the library is external, this here can also help.
– ShutUpMagda
That’s right, man, you have no idea when you helped me, thank you very much, man! I had forgotten this.
– Lucas Lima