zoukankan      html  css  js  c++  java
  • CodeIgniter 定义“全局变量global variable”,可以在所有controller,model和view中使用

       第一次正儿八经用CodeIgniter框架做项目,结果不会定义全局变量,只能在一个controller里定义一个public varable,每个函数调用,别的controller里还需要重新定义,view里还用不了,必须先传值。

           经过研究,在CI中使用全局变量需要自定义Library的形式定义全局变量,这里我介绍一个用config里配置的方法

    一:library/globals.php  

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
                             
    class Globals
    {
                                 
        function __construct($config = array() ){
            foreach ($config as $key => $value) {
                $data[$key] = $value;
            }
                             
            $CI =& get_instance();//这一行必须加
                             
            $CI->load->vars($data);
        }
    }
    

    二:/config/globals.php <-名字必须和library一致,定义具体变量

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
                    
    $config['globals_text'] = "test text";
                    
    $config['globals_text1'] = "test text11";
    

    三:/config/autoload <-配置自动加载项,加载global library

    /*
    | -------------------------------------------------------------------
    |  Auto-load Libraries
    | -------------------------------------------------------------------
    | These are the classes located in the system/libraries folder
    | or in your application/libraries folder.
    |
    | Prototype:
    |
    |   $autoload['libraries'] = array('database', 'session', 'xmlrpc');
    */
          
    $autoload['libraries'] = array('globals');
    

    现在定义了两个变量globals_text和globals_text1就可以在所有MVC中使用了

  • 相关阅读:
    解决在cmd命令下不能输入中文方法
    报错注入
    html表单中的name属性和value属性
    xss漏洞
    DVWA-xss反射型(跨站脚本漏洞)
    DVWA-brute force
    owsap top 10 2017(十大web安全应用程序安全)
    sqli_labs less-5
    盲注
    c++ 类
  • 原文地址:https://www.cnblogs.com/webu/p/2779999.html
Copyright © 2011-2022 走看看