Passing common title to all view and models CodeIgniter

前端 未结 3 723
轻奢々
轻奢々 2021-01-26 09:09

I have this controller



        
相关标签:
3条回答
  • 2021-01-26 09:31

    Here si simple solution and elegant for transfering one variable to all views :)

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Main extends CI_Controller {
    
        //Class-wide variable to store stats line
        protected $title;
    
        function __construct()
        {
            parent::__construct();
            $data->title = "Some site";
            $this->load->vars($data);
        }
    
    0 讨论(0)
  • 2021-01-26 09:48

    I'm using this method in every projects.

    Controller

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Users extends CI_Controller {
    
     //Global variable
     public $outputData = array();  
     public $loggedInUser;
    
    
     public function __construct() {        
            parent::__construct();
     }
    
     public function index()    {
        $this->load->helper('url');
    
        $this->load->view('users/users');
     }
    
    
     public function register() {
    
         parent::__construct();
    
         $this->load->helper('url');
         $this->load->model('users_model');
    
         //get country names
         $countryList = $this->users_model->getCountries();
         $this->outputData['countryList'] = $countryList;   
    
    
         $this->outputData['pageTitle'] = "User Registration";
    
    
        $this->load->view('users/register',$this->outputData);
     } 
    
    }
    

    View file

    <?php if(isset($pageTitle)) echo $pageTitle; ?>
    
    <?php
        if(isset($countryList)){
           print_r($countryList);
        }
    ?>
    
    0 讨论(0)
  • 2021-01-26 09:51

    Before construct function add this:

    public $data = array();
    

    Then in the construct function write:

    $this->data['title']="Somesite";
    

    And finally before load view add this:

    $data = $this->data + $data;
    

    Now you have same $title everywhere.

    0 讨论(0)
提交回复
热议问题