Welcome Guest, Not a member yet? Register   Sign In
Loading Helpers & Libraries in constructors
#1

[eluser]ufasoli[/eluser]
I've been trying to load a few libraries and helpers in the controller constructor, in order to have them available throughout the entire application, but it doesn't work, I've seen a few CI examples where this was done.. and I was wondering if there is something special to be done in order to have them available throughout the entire application instead of loading them every time I write a new function in the controller. I know you can autoload them by adding them in the "autoload.php" file but I would rather not

Thanks

Ulises
#2

[eluser]alpar[/eluser]
you should be able to just load anything in the constructor. Don't forget to call the parent constructor in your controllers constructor ---> parent::Controller();
#3

[eluser]ufasoli[/eluser]
[quote author="alpar" date="1188780504"]you should be able to just load anything in the constructor. Don't forget to call the parent constructor in your controllers constructor ---> parent::Controller();[/quote]

Yes I can load helpers & libraries in the constructor, but if I need the library or helper inside other function, I have to load it again.. what I would like is to only load it once in the constructor and then use it in all the functions without having to load it again..
#4

[eluser]alpar[/eluser]
sorry i wasn't specific enough, that is what i meant. You should be able to use them trough out the code if you load them in the constructor. Maybe showing your controller could be helpful... This has to work, there might be some typo or small thing that you don't see because you are looking for something bigger Smile happens to me all the time Smile
#5

[eluser]ufasoli[/eluser]
[quote author="alpar" date="1188785773"]sorry i wasn't specific enough, that is what i meant. You should be able to use them trough out the code if you load them in the constructor. Maybe showing your controller could be helpful... This has to work, there might be some typo or small thing that you don't see because you are looking for something bigger Smile happens to me all the time Smile[/quote]

Here is my constructor

Code:
function __contruct()
    {
        parent::Controller();
        
        $this->load->helper('url');
                
                
    }
#6

[eluser]Crafter[/eluser]
ufasoli, Your usage is correct. The url helper functions sjould now be available to you in any (other) function of the Class.
#7

[eluser]esra[/eluser]
To make them available across multiple controllers on an application-wide basis, you need to create a base controller (an application controller) and extend all other controllers from that application controller.

A sample Application controller:

Code:
<?php

class Application extends Controller {

    function Application()
    {
        parent::Controller();    
        $this->load->helper( array('link', 'html', 'file') );
        $this->load->library('validation');
        $this->load->library('view');
    }
    
    function index()
    {
         //... local construction code here
    }
}
?>


A controller subclassed from the Application controller:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require(APPPATH.'libraries/application'.EXT);

class Articles extends Application {

    function Articles()
    {
        parent::Application();    
        $this->lang->load('articles', 'en_us');
    }
    
    function index()
    {
        // ... some controller specfic constructor code
        $component = $this->lang->line('articles_component');
        $page = $this->lang->line('articles_title');
        
        $this->view->set('title', $page);
        $this->view->set('comp', $component);
        $this->view->part('nav', 'nav');
        $this->view->part('content', 'content');
        $this->view->part('inner1', 'inner1');
        $this->view->part('inner2', 'inner2');
        $this->view->load('nested');
    }
}
?>

Be sure to add the require line of code to include the application controller in your subclassed controllers. I store my application controllers in libraries, but you can store them in controllers/ or elsewhere by setting the path to suit your needs. You can have multiple base controllers. For example, you could have an Admin controller and create Admin controllers based on it.

In the example above, the helpers and libraries loaded in the Application controller are inheirited by the Articles controller and whatever other controllers are extended from the Application controller.
#8

[eluser]Rick Jolly[/eluser]
[quote author="ufasoli" date="1188786240"][quote author="alpar" date="1188785773"]sorry i wasn't specific enough, that is what i meant. You should be able to use them trough out the code if you load them in the constructor. Maybe showing your controller could be helpful... This has to work, there might be some typo or small thing that you don't see because you are looking for something bigger Smile happens to me all the time Smile[/quote]

Here is my constructor

Code:
function __contruct()
    {
        parent::Controller();
        
        $this->load->helper('url');
                
                
    }
[/quote]

Just a typo with __construct() so the constructor isn't being called in your example.
#9

[eluser]Crafter[/eluser]
Quote:To make them available across multiple controllers on an application-wide basis, you need to create a base controller (an application controller) and extend all other controllers from that application controller.
...Or you coulkd autoload then in the config/autoload.php file.
#10

[eluser]ufasoli[/eluser]
[quote author="Rick Jolly" date="1188793693"][quote author="ufasoli" date="1188786240"][quote author="alpar" date="1188785773"]sorry i wasn't specific enough, that is what i meant. You should be able to use them trough out the code if you load them in the constructor. Maybe showing your controller could be helpful... This has to work, there might be some typo or small thing that you don't see because you are looking for something bigger Smile happens to me all the time Smile[/quote]

Here is my constructor

Code:
function __contruct()
    {
        parent::Controller();
        
        $this->load->helper('url');
                
                
    }
[/quote]

Just a typo with __construct() so the constructor isn't being called in your example.[/quote]

gosh I'm such a dork, I always look for something bigger...

thanks !!!




Theme © iAndrew 2016 - Forum software by © MyBB