Search code examples
phpcodeigniter

Codeigniter global variable problems


I need to use codeigniter global variable, I don't want to create a variable for each controller page, I want to do as we put it in the header in php, but I couldn't find a solution.

Example variable $template;

Controller 1:

    class Auth extends CI_Controller{
        public function index (){
         $this->load->view($template.'Auth')
       }
    }

Controller 2:

    class Loader extends CI_Controller{
        public function index (){
         $this->load->view($template.'Auth')
       }
    }

Solution

  • //-create new file in:-//
    /application/helpers/template_helper.php
    
    <?php 
    function admin_template() {
        return "admin/home";
    }
    
    
    
    //-From controller Auth-//
    
    class Auth extends CI_Controller{
        function __construct(){
            parent::__construct();
            $this->load->helper('template');
        }
            public function index (){
             $data['info']="enter info here....";
             $this->load->view(admin_template().'Auth', $data)
           }
        }
    

    can also auto load (template_helper.php): /application/config/autoload.php