2016-07-20 77 views
1

我正在一個網站,我必須保存一個全局變量。Codeigniter:使用靜態變量

我使用的這個人代碼globals_helper.php custom global variable class

但我總是得到靜態變量null值。

globals_helper.php:

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

// Application specific global variables 
class Globals 
{ 
private static $authenticatedMemberId = null; 
private static $initialized = false; 

private static function initialize() 
{ 
    if (self::$initialized) 
     return; 

    self::$authenticatedMemberId = null; 
    self::$initialized = true; 
} 

public static function setAuthenticatedMemeberId($memberId) 
{ 
    self::initialize(); 
    self::$authenticatedMemberId = $memberId; 
} 


public static function authenticatedMemeberId() 
{ 
    self::initialize(); 
    return self::$authenticatedMemberId; 
} 
} 

我所做的一切就像在幫助文件夾和更新自動加載的文件添加globals_helper.php的步驟。現在,我想自定義庫「Ctrl_utility」功能「get_search_term」訪問這些靜態變量和我的控制器調用get_search_term功能

Ctrl_utility.php

class Ctrl_utility { 
protected $CI; 
public static $static_search = ""; 


public function __construct() 
{ 
    // Assign the CodeIgniter super-object 
    $this->CI =& get_instance(); 

} 

public function get_search_term($searchTerm){ 

    $searchTerm = $this->CI->security->xss_clean(htmlspecialchars($searchTerm)); 

    if (isset($searchTerm) && strlen($searchTerm)>0) { 
     Globals::setAuthenticatedMemeberId($searchTerm); 
    } else { 
     $searchTerm = Globals::authenticatedMemeberId(); 
    } 
    return $searchTerm; 
} 

我的一個控制器,它們都具有類ctrl_utility ,get_search_term函數:

class Blog_controller extends CI_Controller{ 

public function __construct() { 

    parent::__construct(); 

    $this->load->model('blogs_model'); 
} 

public function index(){ 


    //Get SearchTerm Values 
    $searchTerm = $this->ctrl_utility->get_search_term($this->input->post('searchTerm')); 

    //Get Url First Parameter 
    $start = $this->ctrl_utility->get_url_first_parameter(); 

    // Get Data from solr 
    $rows = 10; 
    $data = $this->blogs_model->solrData($start, $rows, $searchTerm); //give start of documents 

    //Pagination 
    $this->pagination->initialize($this->ctrl_utility->pagination_config($this->uri->segment(1), $rows, $data['found'])); 

    //Views 
    $this->load->view('tabs/blogs', $data); 


} 
} 

我做錯了什麼?

+0

你在這裏遇到什麼錯誤? –

+0

當我通過按鈕從一個控制器跳轉到另一個控制器時,我通過調用類Ctrl_utility get_search_term($ searchTerm)函數沒有獲得靜態變量值。 – MTA

回答

0

現在談到在CodeIgniter中定義它們時,有幾種方法。我在下面列出了其中的一些:

  1. 在應用程序/庫中創建屬於自己的文件,其中類構造函數包含一個數組作爲參數。現在在/ application/config中創建一個與應用程序/庫中給出的名稱相同的新文件,並在其中聲明您的全局變量。現在要使用這些變量,自動加載新創建的庫。

  2. 在application/core中創建自己的文件並在其中聲明全局變量。在控制器中,您需要擴展文件名而不是CI_Controller。

  3. 如果全局變量是真的常量,只需將它們添加到application/config/constants.php文件中,並將它們命名爲全部大寫,如同其他定義的那樣。

  4. 如果你想設置應用常量創建新的配置文件並添加變量。現在加載它爲$ this-> config-> load('filename');並訪問這些變量爲

    $ this-> config-> item('variable_name');

而不是創建助手創建庫

第1步:所有的,開放的應用/庫的第一,創建一個自定義 類名globals.php。它包含一個構造函數 包含一個數組作爲參數。

<?php 

class Globals { 

// Pass array as an argument to constructor function 
public function __construct($config = array()) { 

// Create associative array from the passed array 
foreach ($config as $key => $value) { 
$data[$key] = $value; 
} 

// Make instance of CodeIgniter to use its resources 
$CI = & get_instance(); 

// Load data into CodeIgniter 
$CI->load->vars($data); 
} 

} 

?> 

第2步:現在使配置文件,打開應用程序/ config並創建 文件globals.php寫如下的代碼。此文件包含 這將作爲一個數組 Globals類的構造,其中它的鍵和值被存儲在被傳遞的配置變量$數據

<?php 

// Create customized config variables 
$config['web_Address']= 'https://www.example.com/blog'; 
$config['title']= 'CodeIgniter Global Variable'; 

?> 

當構造函數的負載,這將需要的配置變量 從配置文件爲了在任何地方使用這些變量。

注意:上述文件的名稱必須與在 庫文件夾中創建的類相同,否則代碼將不起作用。

第3步:但在使用這些變量之前,我們必須自動加載新建的 創建的庫全局變量,如下所示。

而且在自動加載負荷庫現在

$autoload['libraries'] = array('globals'); 

,您可以使用全局變量在控制器

<?php 
class CI_Global_Variable_Tutorial extends CI_Controller{ 
public function __construct() { 
parent::__construct(); 
} 
// Load view page 
public function index() { 
$this->load->view('show_global_variables'); 
} 
} 
?> 

瀏覽:show_global_variables.php

在視圖頁面,我們可以根據我們的需要使用全局變量。

<?php 
echo "Title of the blog post : ".$title; 
echo "<a href='$web_Address'>"."Click here to go to blog page"."</a>"; 
?> 
+0

load-> vars($ globalv)用於存儲全局變量?它是否像會話,但不存儲在服務器端?我可以重寫$ globalv的值,就像我們在鍵/值對基礎上做的會話一樣嗎? – MTA

+0

按照我上面解釋過的所有步驟進行操作@MTA –

+0

對不起,您可以提出很多問題,我是新手,渴望學習。我怎樣才能從控制器或模型中的代碼一次又一次地覆蓋這些變量,或者這些變量是不變的? – MTA