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

#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:
<?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);
    }
}
?>


Messages In This Thread
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 02:15 AM
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 02:20 AM
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 02:28 AM
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 03:04 AM
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 03:32 AM
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 12:10 PM
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 01:32 PM
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 01:55 PM
Global Var inside Constructor to pass to view - by El Forum - 09-03-2009, 08:22 PM
Global Var inside Constructor to pass to view - by El Forum - 08-17-2011, 06:30 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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