Codeigniter全局变量问题
Codeigniter global variable problems
我需要使用 codeigniter 全局变量,我不想为每个控制器页面创建一个变量,我想像我们在 php 中的 header 中那样做,但是我找不到解决方案。
示例变量 $template
;
控制器 1:
class Auth extends CI_Controller{
public function index (){
$this->load->view($template.'Auth')
}
}
控制器 2:
class Loader extends CI_Controller{
public function index (){
$this->load->view($template.'Auth')
}
}
//-create new file in:-//
/application/helpers/template_helper.php
<?php
function admin_template() {
return "admin/home";
}
//-From controller Auth-//
class Auth extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('template');
}
public function index (){
$data['info']="enter info here....";
$this->load->view(admin_template().'Auth', $data)
}
}
也可以自动加载(template_helper.php): /application/config/autoload.php
您也可以在app/core下创建一个基础控制器(例如BASE_Controller.php);然后在此处(在基本控制器上)定义您的全局变量。
之后,只需从控制器扩展基本控制器。
这种方法也可以用于全局功能,例如检查用户是否登录、获取一些全局数据(如设置等)。
请参阅以下示例:
app/core/BASE_Controller.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class BASE_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->data['info'] = "Your value goes in here";
// if the info is declared from a function, do as:
$this->data['logged_in'] = $this->checkStatus();
}
function checkStatus(){
return false;
}
}
然后在 app/controller/Controller.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends BASE_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view($template.'Auth',$this->data);
}
}
您可以简单地从 $array_index 的视图中访问您的变量;例如,如果您定义了 $this->data['info'],访问方式为:
$info;
//likewise, if you have $this->data['user_id'], access it as:
$user_id;
扩展基本控制器的前控制器,只需按以下方式访问它:
//$this->data['variable'];
//example;
$user_id = $this->data['user_id'];
我需要使用 codeigniter 全局变量,我不想为每个控制器页面创建一个变量,我想像我们在 php 中的 header 中那样做,但是我找不到解决方案。
示例变量 $template
;
控制器 1:
class Auth extends CI_Controller{
public function index (){
$this->load->view($template.'Auth')
}
}
控制器 2:
class Loader extends CI_Controller{
public function index (){
$this->load->view($template.'Auth')
}
}
//-create new file in:-//
/application/helpers/template_helper.php
<?php
function admin_template() {
return "admin/home";
}
//-From controller Auth-//
class Auth extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('template');
}
public function index (){
$data['info']="enter info here....";
$this->load->view(admin_template().'Auth', $data)
}
}
也可以自动加载(template_helper.php): /application/config/autoload.php
您也可以在app/core下创建一个基础控制器(例如BASE_Controller.php);然后在此处(在基本控制器上)定义您的全局变量。 之后,只需从控制器扩展基本控制器。 这种方法也可以用于全局功能,例如检查用户是否登录、获取一些全局数据(如设置等)。 请参阅以下示例:
app/core/BASE_Controller.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class BASE_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->data['info'] = "Your value goes in here";
// if the info is declared from a function, do as:
$this->data['logged_in'] = $this->checkStatus();
}
function checkStatus(){
return false;
}
}
然后在 app/controller/Controller.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends BASE_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view($template.'Auth',$this->data);
}
}
您可以简单地从 $array_index 的视图中访问您的变量;例如,如果您定义了 $this->data['info'],访问方式为:
$info;
//likewise, if you have $this->data['user_id'], access it as:
$user_id;
扩展基本控制器的前控制器,只需按以下方式访问它:
//$this->data['variable'];
//example;
$user_id = $this->data['user_id'];