Welcome Guest, Not a member yet? Register   Sign In
helper - controller - function dilemma
#1

[eluser]webscriptz.be[/eluser]
Hi,

I'm making myself a webhosting system and I'm think I'm ding overkill in lines of codes actually. I have some functions that every single function needs having but having 10 functions in your controller for 10 different think gets the document kind of crowded so can i best do this in a helper or something, note that i need the the corresponding models to because otherwise it won't work:

the concerning bits of code, there all placed in the model settings at the moment.

Code:
$data['links'] = $this->Settings->getLinks();
        $data['config'] = $this->Settings->getConfig();
        $data['lang'] = $this->Settings->getLang();
        $this->load->vars($data);

So please can anybody help me.

-Tim
#2

[eluser]TheFuzzy0ne[/eluser]
Your function is out of context of the CI Super Object. You need to reference it with get_instance():

Code:
$CI =& get_instance();

$data['links']  = $CI->Settings->getLinks();
$data['config'] = $CI->Settings->getConfig();
$data['lang']   = $CI->Settings->getLang();
$this->load->vars($data);
#3

[eluser]webscriptz.be[/eluser]
[quote author="TheFuzzy0ne" date="1238510689"]Your function is out of context of the CI Super Object. You need to reference it with get_instance():

Code:
$CI =& get_instance();

$data['links']  = $CI->Settings->getLinks();
$data['config'] = $CI->Settings->getConfig();
$data['lang']   = $CI->Settings->getLang();
$this->load->vars($data);
[/quote]

if it's a helper or library, the code i posted is situated in the controllers. sorry if i wasn't clear on that.
#4

[eluser]zutis[/eluser]
Third reply down here :

http://ellislab.com/forums/viewthread/108293/

... this is a good way to give all your models, controllers and libs access to site wide vars etc.
#5

[eluser]xwero[/eluser]
If i understand it correctly links, config and lang come out of the settings model and you needs those variables in all your controller functions?

If this is right you can use class variables and use the constructor to set them
Code:
class Some_controller extends Controller
{
    var $links;
    var $config;
    var $lang;

    function Some_controller()
    {
        $this->load->model('settings');
        $this->links = $this->Settings->getLinks();
        $this->config = $this->Settings->getConfig();
        $this->lang = $this->Settings->getLang();
    }
}
If you need the variables in your views then you can use the vars method
Code:
class Some_controller extends Controller
{
    function Some_controller()
    {
        $this->load->model('settings');
        $data['links'] = $this->Settings->getLinks();
        $data['config'] = $this->Settings->getConfig();
        $data['lang'] = $this->Settings->getLang();
        $this->load->vars($data);
    }
}
#6

[eluser]webscriptz.be[/eluser]
[quote author="xwero" date="1238511892"]If i understand it correctly links, config and lang come out of the settings model and you needs those variables in all your controller functions?

If this is right you can use class variables and use the constructor to set them
Code:
class Some_controller extends Controller
{
    var $links;
    var $config;
    var $lang;

    function Some_controller()
    {
        $this->load->model('settings');
        $this->links = $this->Settings->getLinks();
        $this->config = $this->Settings->getConfig();
        $this->lang = $this->Settings->getLang();
    }
}
If you need the variables in your views then you can use the vars method
Code:
class Some_controller extends Controller
{
    function Some_controller()
    {
        $this->load->model('settings');
        $data['links'] = $this->Settings->getLinks();
        $data['config'] = $this->Settings->getConfig();
        $data['lang'] = $this->Settings->getLang();
        $this->load->vars($data);
    }
}
[/quote]

it's the second patch of code that i have in every function of my controllers, so i would like to make in into a main method or something alike so that i don't need to declare them nor that i need to put it in each individual function.
#7

[eluser]pixelazion[/eluser]
I don't know if this will help. ^_^
but you can try this for a start
Code:
$this->load->model('settings'); <-- small s on settings
$this->links = $this->Settings->getLinks(); <--- capital S for Settings, try lower case
#8

[eluser]webscriptz.be[/eluser]
I going to explain myself a little more, it think it's my fault you guys don't understand it as i have got it in mind, for which i'm sorry.

I have to code (see first post), this code is located in the controllers and has to be declared in every function of each controller with exception of the constructor.

My problem now is that is not really a nice view nor is it a good practice.

now i have done some trail and error and i came up with the following construction which is already sowhat better but still there's room for improvement.

Code:
&lt;?php
class Frontpage extends Controller {
    
    var $data = array();
    
    function Frontpage(){
        parent::Controller();
        
        $data['links'] = $this->Settings->getLinks();
        $data['config'] = $this->Settings->getConfig();
        $data['lang'] = $this->Settings->getLang();        
    }
    
    function index(){    
        $this->load->vars($data);
        $this->load->view('default/frontpage/index');
    }
?&gt;

the only thing which still buggers me slightly is:
Code:
$this->load->vars($data);
the fact that i have to put it in every function for the view and yes, foolishly believing the constructor would do the job i tried but it didn't do the job so is there still another way i can achieve this?
#9

[eluser]zutis[/eluser]
I think I understand what you mean. And my first response still stands. Try it.

Your code would look something like this:

first create application/libraries/MY_Controller.php

And put this in:

Code:
&lt;?

class MY_Controller extends Controller
{
   function MY_Controller()
   {
       parent::Controller;
    
       $this->load_common_variables();
   }

   function load_common_variables()
   {
        $this->load->model('settings');
        $data['links'] = $this->settings->getLinks();
        $data['config'] = $this->settings->getConfig();
        $data['lang'] = $this->settings->getLang();
        $this->load->vars($data);
   }
}

?&gt;

then your code will look like this:

Code:
&lt;?php
class Frontpage extends MY_Controller
{  
    function Frontpage(){
        parent::Controller();    
    }
    
    function index(){    
        $this->load->view('default/frontpage/index');
    }
?&gt;
note the extends MY_Controller - that is the important bit.

Everything else will work the same.
#10

[eluser]webscriptz.be[/eluser]
Thanks! it works the only thing that apparently i had to add to the individual controllers was $this->load_common_variables(); in the constructor




Theme © iAndrew 2016 - Forum software by © MyBB