Php Codeigniter:使用静态变量
我正在制作一个网站,我必须保存一个全局变量 我正在使用这个人代码globals_helper.php 但我总是得到静态变量值null globals_helper.php:Php Codeigniter:使用静态变量,php,codeigniter,Php,Codeigniter,我正在制作一个网站,我必须保存一个全局变量 我正在使用这个人代码globals_helper.php 但我总是得到静态变量值null globals_helper.php: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); // Application specific global variables class Globals { private static $authentic
<?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;
}
}
我的一个控制器,它们都有类ctrl\u实用程序,get\u search\u 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);
}
}
我做错了什么吗?现在,在CodeIgniter中定义它们时,有几种方法可以做到这一点。我在下面列出了其中一些:
<?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
在视图页面中,我们可以根据需要使用全局变量
现在,当在CodeIgniter中定义它们时,有几种方法可以做到这一点。我在下面列出了其中一些:
<?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
在视图页面中,我们可以根据需要使用全局变量
您在这里遇到了什么错误?当我通过按钮从一个控制器跳到另一个控制器时,我不会通过调用类Ctrl\u实用程序get\u search\u term($searchTerm)来获取静态变量值函数。您在这里遇到了什么错误?当我通过按钮从一个控制器跳到另一个控制器时,调用类Ctrl\u实用程序get\u search\u term($searchTerm)函数无法获取静态变量值。load->vars($globalv)用于存储全局变量?它是否像一个会话,但不存储在服务器端?我可以像在基于键/值对的会话中那样覆盖$globalv值吗?按照我上面解释的所有步骤进行操作@MTA很抱歉问您这么多问题,我是新手,渴望学习。如何从控制器或模型等代码中一次又一次地重写这些变量,或者这些变量是常量?浏览用户指南,它将帮助您了解所有CornerLoad->vars($globalv)用于存储全局变量?它是否像一个会话,但不存储在服务器端?我可以像在基于键/值对的会话中那样覆盖$globalv值吗?按照我上面解释的所有步骤进行操作@MTA很抱歉问您这么多问题,我是新手,渴望学习。我如何从控制器或模型中的代码中一次又一次地重写这些变量,或者这些变量是常量?请阅读用户指南,它将帮助您了解所有方面
<?php
// Create customized config variables
$config['web_Address']= 'https://www.example.com/blog';
$config['title']= 'CodeIgniter Global Variable';
?>
$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');
}
}
?>
<?php
echo "Title of the blog post : ".$title;
echo "<a href='$web_Address'>"."Click here to go to blog page"."</a>";
?>