• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Var inside Constructor to pass to view

#1
[eluser]pyrokinesis[/eluser]
Hi everyone,

Is it possible to automatically declare/set a variable in the controller __construct and automatically pass it to the view when every controller method is called???

E.G. ->

Code:
<?
class Sample_Controller extends Controller {
    
    public $data;

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

        $this->data = array();
        $data['active_menu'] = 'Menu One';
    }
    
    public function function_one($data) {
        $data['page_title'] = 'Sample Page Title';
        //$data['active_menu'] is passed to the main view
        $this->load->view($this->main_view, $data);
    }

    public function function_two($data) {
        $data['page_title'] = 'Another Page Title';
        //Override the default $data['active_menu'] value
        $data['active_menu'] = 'new value';
        $this->load->view($this->main_view, $data);
    }
}
?>

Thanks

#2
[eluser]Dam1an[/eluser]
You would need to use $this to reference the data array in all your functions, like so
Code:
<?php
class Sample_Controller extends Controller {
    
    public $data;

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

        $this->data = array();
        $this->data['active_menu'] = 'Menu One';
    }
    
    public function function_one() {
        $this->data['page_title'] = 'Sample Page Title';
        //$data['active_menu'] is passed to the main view
        $this->load->view($this->main_view, $this->data);
    }

    public function function_two() {
        $this->data['page_title'] = 'Another Page Title';
        //Override the default $data['active_menu'] value
        $this->data['active_menu'] = 'new value';
        $this->load->view($this->main_view, $this->data);
    }
}

Also, passing $data into the function is a bad idea, unless you plan on having serialized arrays in the URL? Wink

#3
[eluser]pyrokinesis[/eluser]
Thanks for the reply Dam1an,

How about if I do this then?

Code:
<?php
class Sample_Controller extends Controller {

    public function __construct() {
        parent::__construct();
        $this->data['active_menu'] = 'Menu One';
    }
    
    public function function_one() {
        $this->data['page_title'] = 'Sample Page Title';
        //$data['active_menu'] is passed to the main view
        $this->load->view($this->main_view, $this->data);
    }

    public function function_two() {
        $this->data['page_title'] = 'Another Page Title';
        //Override the default $data['active_menu'] value
        $this->data['active_menu'] = 'new value';
        $this->load->view($this->main_view, $this->data);
    }
}

It doesn't seem to work either,

I'm trying to set up a default value that is set in the constructor and passed passed to every view unless overwritten from within the specific controller.

Thanks for the help Smile

#4
[eluser]n0xie[/eluser]
[quote author="pyrokinesis" date="1251984496"]

I'm trying to set up a default value that is set in the constructor and passed passed to every view unless overwritten from within the specific controller.
[/quote]

You would be better of just extending the Controller Class. In MY_Controller you set all your default values, and unless overwritten from within your controller, it will use those values. It's easier and cleaner.
http://ellislab.com/codeigniter/user-gui...asses.html

#5
[eluser]pyrokinesis[/eluser]
Thanks for the help guys...

Ended up opting for the simple solution:

Code:
<?
class Sample_Class extends Controller {
    
    private $active_menu_item;

    public function __construct() {
        parent::__construct();
        $this->active_menu_item = 'Menu Item One';
    }
    
    public function function_one() {
        $data['page_title'] = 'Page Title';
        $data['active_menu_item'] = $this->active_menu_item;
        $this->load_main_view($data);
    }
?>

Thanks

#6
[eluser]maicobreako[/eluser]
I was having trouble getting MY_Controller to work and ran across this thread.
My question is why does parent::__construct(); work? The only referrence to this I've seen, is this thread.

I couldn't get parent::MY_Controller(); to work in the Contact controller, I'd get this message:

"Fatal error: Call to undefined method MY_Controller::MY_Controller() in C:\wamp\winf\contact\controllers\contact.php on line 8"

BTW, using ci svn version from a few weeks ago.

/winf/contact/libraries/MY_Contoller.php
Code:
class MY_Controller extends Controller
{
    function __construct()
    {
        parent::Controller();
        <<snipped>>
    }
}
/winf/contact/controllers/contact.php
Code:
class Contact extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url', 'form', 'html'));
        $this->load->library(array('form_validation', 'session'));
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    }

<<snipped>>
}

#7
[eluser]maicobreako[/eluser]
To answer my own question, apparently I too quickly skimmed over this line in the user guide.
Quote:Note: If you need to use a constructor in your class make sure you extend the parent constructor:

#8
[eluser]Chad Fulton[/eluser]
It looks like this thread has moved away from the original question, but here's an alternate method:

Quote:Is it possible to automatically declare/set a variable in the controller __construct and automatically pass it to the view when every controller method is called???

You can do this via the $this->load->vars(); function. I'll modify your original example to demonstrate:

Code:
&lt;?php
class Sample_Controller extends Controller {
    
    public $data;

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

        // All views that are called after this is called will
        // have access to the $active_menu variable
        $this->load->vars(
            array(
                'active_menu'=>'Menu One',
            )
        );
    }
    
    public function function_one($data) {
        $data['page_title'] = 'Sample Page Title';

        // This view will have access to $active_menu even
        // though it wasn't defined in this function.
        $this->load->view($this->main_view, $data);
    }

    public function function_two($data) {
        $data['page_title'] = 'Another Page Title';

        // Since we want a different value for $active_menu, we
        // can override it just as you have done here.
        $data['active_menu'] = 'new value';
        $this->load->view($this->main_view, $data);
    }
}
?&gt;

#9
[eluser]Colin Williams[/eluser]
+1 for $this->load->vars()

Best function for the job.

#10
[eluser]sineld[/eluser]
Code:
&lt;?php
        $this->load->vars(
            array(
                'active_menu'=>'Menu One',
            )
        );
?&gt;

this lines saved my life :-)

thanks


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.