【问题标题】:Codeigniter: Using static variableCodeigniter:使用静态变量
【发布时间】:2016-07-20 07:12:38
【问题描述】:

我正在制作一个网站,我必须在其中保存一个全局变量。

我正在使用此人代码 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);


}
}

我做错了吗?

【问题讨论】:

  • 你遇到了什么错误?
  • 当我通过按钮从一个控制器跳转到另一个控制器时,我没有通过调用类 Ctrl_utility get_search_term($searchTerm) 函数获得静态变量值。

标签: php codeigniter


【解决方案1】:

现在在 CodeIgniter 中定义它们时,有几种方法可以做到这一点。我在下面列出了其中一些:

  1. 在应用程序/库中创建您自己的文件,其中类构造函数包含一个数组作为参数。现在在 /application/config 中创建一个与 application/libraries 中相同名称的新文件,并在其中声明全局变量。现在要使用这些变量,请自动加载新创建的库。

  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 步:现在制作配置文件,打开 application/config 并创建 文件为 globals.php 并编写下面给出的代码。该文件包含 将作为数组传递给构造函数的配置变量 其键和值存储在 $data 中的全局类

<?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>";
?>

【讨论】:

  • load->vars($globalv) 用于存储全局变量?它像会话但不存储在服务器端吗?我可以像在会话中基于键/值对那样覆盖 $globalv 值吗?
  • 按照我上面解释的所有步骤操作@MTA
  • 很抱歉问你这么多问题,我是新手,渴望学习。如何从控制器或模型中的代码中一次又一次地覆盖这些变量,或者这些变量是常量?
  • 阅读用户指南codeigniter.com/user_guide它会帮助你所有的角落
猜你喜欢
  • 1970-01-01
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多