CodeIgniter Forums

Full Version: Global functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]mabright[/eluser]
I'm probably over thinking and missing the obvious but I have a common template view that I call from every controller and I include other view data in the template view based on page. The template view has a header area that I want displayed on every page. In the header area I have a section that displays data based on IP address and the data is dynamically determine so I cannot just hard code a global variable in the config.php file.

I there a way to define some global include/function and have it called from the config.php file to build my dynamic content and put it in a global variable so that it can be used in my template view?

El Forum

[eluser]mddd[/eluser]
Why does that need to be in a global variable? You are getting other data for the rest of the page. Just get the header data around the same time you get the page data and load them both to the template.

El Forum

[eluser]mabright[/eluser]
I decided to create a helper, load it in autoload.php and call the needed function from the template view. I knew I was over thinking this. I haven't touched CI or PHP in awhil, I'm refreshing my memory on CI, MVC and PHP.

Gracias...

El Forum

[eluser]mabright[/eluser]
I need to call a method in one of my custom libraries. Can I include the library or call it from the helper file?

El Forum

[eluser]mddd[/eluser]
Helpers are not really supposed to call CI related objects/methods (although it is possible).
I think the best (most logical) way would be to include the library in the Controller. Maybe you can integrate the function from the helper into the library (if they are related of course). Anyway, if you do all the 'research' from the controller by calling models/libraries, you could pass the end result to the view. Eliminating the need to call complex routines from the view or from the helper at all.

El Forum

[eluser]mabright[/eluser]
This is what I am doing now, I get the header data in my controller and pass to the view. However I do not want to gather the same data in each of my controllers and pass to the common view.

Before I started using CI, I would have a global file with global variables and functions and in all my pages I could just use global variables instead of having to build the data in each page.

I want to replicate this in CI. I thought I could create a helper to do this by the data I am trying to retrieve using one of my custom library files and I cannot include it in the helper.

Can or should I call my library class from the view directly?

El Forum

[eluser]mddd[/eluser]
You can. If you use this code in your view.
Code:
$CI =& get_instance();
// $CI can now be used the same as you would use $this in your controller.
// for instance:
$CI->my_library->my_function();

Whether you want to do it this way or a different way is mainly a matter of taste.
Somehow it feels better to me to put the libray call in the controller and pass the data to the view.
I think it's because I like to keep the view as 'dumb' as possible.
For instance:
Code:
// this is in the controller
$this->load->library('my_library');
$data = $my_library->get_my_data();
$this->load->view('my_view', $data);
If you wanted to make that shorter you could autoload the library. You could even set the data from the library using
Code:
$this->load->vars($data); // after this, $data is pushed to every view you load!
This would automate it completely; you wouldn't have to do anything in the controller or the view, to have the data available.

El Forum

[eluser]mabright[/eluser]
Perfect, I will auto-load and set from the library. This is what I wanted. I like to follow standards and keep the view dumb as well and I hate to add the same functionality to each controller.

Thanks.

El Forum

[eluser]mabright[/eluser]
I have made the changes and I am going to load the variable in my library but my library class uses the CI session class which I autoload. However, the session class is no recognized in my library class.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Location::$session

Filename: libraries/Location.php

Line Number: 31

Fatal error: Call to a member function userdata() on a non-object in /home/content/html/libraries/Location.php on line 31

I tried to load it into the class but that did not work. How do I reference the session class in my library class?

El Forum

[eluser]danmontgomery[/eluser]
Code:
$CI =& get_instance();
$CI->session->etc();
Pages: 1 2