【问题标题】:Passing common title to all view and models CodeIgniter将通用标题传递给所有视图和模型 CodeIgniter
【发布时间】:2013-06-11 05:59:05
【问题描述】:

我有这个控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

     function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('text');

    }

    public function index()
    {
        $this->home();
    }

    public function home()
    {
            $data['title']="Somesite";
        $this->load->view("view_home", $data);

    }

        public function blog()
    {
            $data['title']="Somesite";
        $this->load->view("view_blog", $data);

    }
        public function answers()
    {
            $data['title']="Somesite";
        $this->load->view("view_answers", $data);

    }
    }

你可能看到$data['title']对于所有函数都是一样的,如何让它更简单,在开始时包含而不是在每个函数中都写,再次重复,然后转移到查看。 有没有办法将其转移到功能上?

【问题讨论】:

    标签: php codeigniter templates variable-assignment


    【解决方案1】:

    在构造函数之前添加:

    public $data = array();
    

    然后在构造函数中写:

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

    最后在加载视图之前添加:

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

    现在你到处都有相同的 $title

    【讨论】:

    • 不走运,消息:未定义变量:标题
    • 嗯。我在我的每个项目中都使用它。你能提供新代码让我检查吗?
    • 我提供了更优雅的解决方案,请看一下:)
    【解决方案2】:

    这里是一个简单的解决方案,优雅地将一个变量转移到所有视图:)

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

    【讨论】:

      【解决方案3】:

      我在每个项目中都使用这种方法。

      控制器

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

      查看文件

      <?php if(isset($pageTitle)) echo $pageTitle; ?>
      
      <?php
          if(isset($countryList)){
             print_r($countryList);
          }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-29
        • 2019-02-01
        • 1970-01-01
        • 2011-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多