在 codeigniter 中从父 MY_Controller 调用变量

Calling a variable from parent MY_Controller in codeigniter

我想让我的徽标成为全球徽标,以便我可以在我网站的任何位置访问它。为此,我在自定义父控制器 MY_Authorization 中声明 $logo 并且每个 class 都由该自定义控制器扩展。

但是当我在我的视图中调用 $logo 变量时,发生 php 未定义变量的错误:$logo

defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Authorization extends CI_Controller {
var $logoCont='logo';
    function __construct()
    {
    parent::__construct();
    $CI = & get_instance();
    $CI->load->library('session');
    $CI->load->helper('url');
    if ( !$this->session->userdata('loginuser'))
    { 
        redirect('login');
    }
  }
}

在我看来,我是这样称呼徽标的

  <img src="<?php echo base_url().sprintf("uploads/%s", $logoCont)?>"alt="logo">

创建自定义帮助文件并放置在帮助文件夹中

  1. 导航至 application/helper
  2. 列表项
  3. 创建新的自定义文件

示例:my_custom_helper.php

function logo(){
   $ci = & get_instance();
   $result = $ci->db->query("you custom query here");
   //fetch the logo from database and store in the variable  and return this variable 
   return $result;
}
  1. (application/config/autoload.php)

    autoload.php 文件中自动加载此助手=40=]
  2. 找到变量$autoload['helper'] = array();

  3. 在那里添加您的自定义助手,例如 $autoload['helper'] = array('my_custom_helper.php);

  4. 现在 logo() 函数是全局的,它将在 controller/model/view 中随处可用,并且每当对辅助函数进行更改时,它都会影响 [=13= 中的应用程序] 函数被使用。

您的数据未传递到视图。

if ( !$this->session->userdata('loginuser'))
{ 
    ---added this---
    $this->session->set_flashdata('item', 'value');
    // OR $this->session->set_flashdata($data); $data is of type array
    ----------------
    redirect('login');
}

现在您的数据已在会话中设置(并将在下一次请求后删除),您可以使用 $this->session->flashdata('item') 在视图中访问此数据。

https://codeigniter.com/user_guide/libraries/sessions.html#flashdata

希望对您有所帮助。