How to instantiate an object in Hooks to use in the CodeIgniter Controller?

0

I have a class that will manage some of my dependencies. I want it to be called before any method is called in the controller , and for this I am using hooks . The problem is that I do not know how to call this object that has already been instantiated in hook in my controller methods. How could I do that? Here's how it is:

In I'm using this configuration:

$hook['post_controller_constructor'][] = array(
    'class' => 'DependencyInjection',
    'function' => 'initContainer',
    'filename' => 'DependencyInjection.php',
    'filepath' => 'hooks');

And the dependencia.php file looks like this:

<?php
use Pimple\Container;

$container = new Container();

$container['guzz'] = function($c) {
    return new GuzzleHttp\Client();
};

hook was created like this:

<?php
class DependencyInjection {

    public $container;

    public function initContainer () {
        return $this->container = require_once '/var/www/projetoSize/dependencia.php';
    }
}

I want to use this in my controller like this:

class Teste extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        var_dump($this->guzz);
    }
}

But it is giving an error with the following message:

  

Message: Undefined property: Test :: $ guzz

    
asked by anonymous 18.08.2017 / 17:23

1 answer

1

If you need a class that serves methods in all instances of the application, then you must use a library. At least one helper . The hook is the very same Core extension of CodeIgniter, ie it is the class itself, and you should not use it for load another class. This may even work, but that is not the ideal tool function.

- To use a helper:

A helper is a script that contains a collection of functions to help you perform basic tasks. CodeIgniter already has a number of such functions, and before trying to create something, it might be interesting to check if what you want is no longer there. Search the native helpers collection.

When you create a helper and load it into the application, it will be available in all instances:

  

CodeIgniter does not load Helper Files by default, so the first step   in using Helper is to load it. Once loaded, it becomes globally   available in your controller and views .

However, a helper will not receive requests directly, in MVC it is always the control layer that handles this kind of thing. So you should call your helper function within a method of a controller .

To understand how to create a helper and how it works minimally, read the "Enabling and creating a helper" excerpt in this answer .

- To create a library:

According to references references, you you can create a class and instantiate it in the controller you want to use:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Someclass {

        public function some_method(){

        }
}

Then just load the class in controller :

$this->load->library('someclass');

Or load with autoload :

>
$autoload['libraries'] = ['someclass'];

- Uploading a third-party library: ( reference )

After downloading the library to a location on your server declare it exists. Create a file within application / libraries / Classname.php :

<?php
class Classname {
    function __construct() {
        require_once '/*path_to_classname.php*';
    }
}

Click autoload and call anywhere in the application (in a controller )

function some_function(){
    $func = new Classname();
    (...)
}
    
21.08.2017 / 16:10