Welcome Guest, Not a member yet? Register   Sign In
Global variables in controllers
#1

[eluser]dpgtfc[/eluser]
Hopefully I can explain this well, but it seems the controllers don't act as a regular class. I can't create a protected variable within the class and make setters and getters.

The problem I have is the repetition of code. For example I have a controller named Home, of which has several functions for each page, "home", "about", "contact" etc. I'm also using the template class, though that doesn't matter too much I don't think.

Anyway, on some of these I have several repeating variables, such as $data['content'] but some that change, such as $data['title']. I tried instantiating the array in the constructor, but that doesn't work. So like regular OO Php, I tried making a protected (switched to just var) outside of the constructor and tried using this:

Code:
class Home extends Controller{
    
    var $arr;

    function Home()
    {
        parent::Controller();
        $tmp = array();
        $this->load->library('parser');
        $this-arr = array(
        "content"=>""content" => $this->parser->parse('pages/conte/home_content_view',$tmp, TRUE)");
        

    }
    function index(){
    ... etc


Right now I have about 6 variables stored in the array, and on about half of the pages, 3-4 of the variable are the same. Some variables in the template I have blank, such as the admin menu which will only show up if you are logged in as admin. So I'd like to instantiate those variables as blank, such as $data['admin'] = "";

So how do I go about doing this, I've made my own classes before and I was always able to do things like $this->arr = $myArray; but I get "unexpected =" errors.

Is it possible to create an assoc array that is used by all functions in a particular controller class? Or do I have to repeat my code in each function? This makes making any changes a bit tedious, as I have to change every function and doesn't seem very scalable. This is what OOP is supposed to take care of, right?
#2

[eluser]drewbee[/eluser]
Create a configuration file. Would seem to work perfectly for this.
#3

[eluser]drewbee[/eluser]
*DELETE* double post.
#4

[eluser]Sumon[/eluser]
How is the idea if we use global varaibale and library like \application\libraries\common_tasks.php
Code:
class Common_tasks{
  function Common_tasks()
  {
     global $data;
     $data['admin'] = ''; // here goes all variables with default values or even value from database
  }
}
Now autoload common_tasks library in config\autoload.php and home controller like
Code:
class Home extends Controller{
    function Home()
    {
        parent::Controller();
        global $data; //Automatically load default values
        $data['admin'] = 'some value';
    }
    function index(){
    ... etc
The main difference between using global variable and data through config i found is: config variable cant set value from database. This is why i like to use it.
#5

[eluser]drewbee[/eluser]
True. I didn't catch on to that (read it to fast). One other option is to actually extend the CI Controller.

Code:
class MY_Controller extends Controller
{
    var $myCommonVar;

    function __construct()
    {
        parent::__construct();
    }
}

Then we have our new controller:

Code:
class Home extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }
}

In the above controller, we now have access to myCommonVar by doing a $this->myCommonVar;

Hope this makes sense.
#6

[eluser]nguyenbatbinh[/eluser]
I don't understand used global variable in CI (used in all function in controller).
I take a exemple like Sumon:
Library: common_tasks.php
Code:
<?php
class common_tasks{
    function common_tasks()
    {
        global $data;
        $data['logged_in']=false;
    }
}    

?>
Controller: blog.php
Code:
<?php

class Blog extends Controller
{
    function Blog()
    {
        parent::Controller();
        //  load
        $this->load->helper(array('url','form','security'));
        $this->load->model(array('mblog'));
        $this->load->database();
        $this->load->library(array('auth','session','common_tasks'));
        $this->load->scaffolding('entries');

        // global variable
        global $data; // automaticaly load default values
        $data['logged_in']=true;
    }
        function index()
    {
        $data['title']='MY BLOG';
        $data['heading']='WELLCOME TO DISCONTENED BLOG';
        // lay du lieu trong bang entries
        $this->db->order_by("entry_id","desc");
        $data['query']=$this->db->get('entries');
        // Hien thi noi dung bang cach load bblog_view.php voi cac bien $data
        $this->load->view('blog_view',$data);
        
    }
        function secure()
    {
        $data['title']='Secure';
        $data['heading']='SECURE';
        if(!$this->auth->logged_in())
        {
            redirect('blog/login');
        }else
        {
            $this->load->view('secure_view',$data);    
        }
        
    }
  }
?>
View: secure_view.php
Code:
<?php
$this->load->view('header');
?>
<div id="box">
    <div class="title_box">
    <h2>The secure area!</h2>
    </div>
    <div class="body_box">

    <p>&lt;?=$logged_in?&gt;</p>

    &lt;?=anchor('blog','Return blog page');?&gt; | &lt;?=anchor('blog/logout','or logout');?&gt;
    </div>
    
</div>

&lt;?php
$this->load->view('footer');
?&gt;

Big Grin It not work.. And a PHP Error was encountered
Quote:Severity: Notice

Message: Undefined variable: logged_in

Filename: views/secure_view.php

Line Number: 9

My code true or false, and can you tell me the way to fix and use global varialle.

____________________
Sorry! I speak english not good!
#7

[eluser]Sumon[/eluser]
@nguyenbatbinh:
would you please add
Code:
global $data; // automaticaly load default values
in first line of secure() function. i hope it will remove PHP Error
#8

[eluser]nguyenbatbinh[/eluser]
Oh yes, It works!
That mean, in each function if we want used $data['logged_in'] we must add global $data into the function?
Thank you very much!
#9

[eluser]wiredesignz[/eluser]
I'm sorry but globals are for lame coders, not for elite CI empowered programmers like us.

Just because something works, it doesn't mean it's the right thing to do.

Have a nice day :lol:




Theme © iAndrew 2016 - Forum software by © MyBB