CodeIgniter Forums
$Variable available in all controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: $Variable available in all controllers (/showthread.php?tid=15419)



$Variable available in all controllers - El Forum - 02-04-2009

[eluser]hugle[/eluser]
Hello Everyone.

I want to call :
Code:
$this->load->helper('date');
$datestring = "%l, %j %F, %G:%i";
$this->data['nav_time'] = mdate($datestring, time());

in every of of controller, so I have $this->data['nav_time'] value in every controller.
I understood that I need to use a hook in pre_controller probably?

But can't understand really how is it done ...
I have looked into howto - but no good resultSad

Anyone can point me on this?
Thank you!


$Variable available in all controllers - El Forum - 02-04-2009

[eluser]davidbehler[/eluser]
You could extend the controller class. Maybe this may work:

Code:
class MY_Controller extends Controller {

    var data;

    function My_Controller()
    {
        parent::Controller();
        $this->load->helper('date');
        $datestring = "%l, %j %F, %G:%i";
        $this->data['nav_time'] = mdate($datestring, time());
    }
}

And in your controllers you now have to extend MY_Controller instead of Controller class and in every one of your controllers the $this->data variable should be available.


$Variable available in all controllers - El Forum - 02-04-2009

[eluser]xwero[/eluser]
why not autoload a custom config file with
Code:
$config['nav_time'] = date('l, j F, G:i');
Or add following to the constants.php file
Code:
define('NAV_TIME', date('l, j F, G:i'));

The mdate function is only useful if you have text inside the dateformat, take a look at the example in the user guide.


$Variable available in all controllers - El Forum - 02-04-2009

[eluser]hugle[/eluser]
Hello again waldmeister and xwero.

I have tried both of your suggestions - both seems to work Smile
Thanks again guys for such a great support !