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

Asked

Viewed 565 times

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

  • 1

    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.

  • That’s right, man, you have no idea when you helped me, thank you very much, man! I had forgotten this.

1 answer

1


If you need a class that serves methods in all instances of the application, then you should use a library. At least one helper. The hook is the very extent of Core of Codeigniter, I mean, he’s the class itself, and you shouldn’t use him to carry another class. This may work, but that’s not the ideal tool function.

- To use a helper:

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

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 a 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 is always the control layer that deals with this kind of thing. So, you should call your function helper within a method of a controller.

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

- To create a library:

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

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

class Someclass {

        public function some_method(){

        }
}

Then it’s enough carry the class at controller:

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

Or carry autoload:

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

- Loading a third party library: (reference)

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

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

Load with autoload and call anywhere in the app (in a controller or in a helper):

function some_function(){
    $func = new Classname();
    (...)
}
  • I’ll test it here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.