【问题标题】:Why $params gets the values from configuration file and not the value set as default?为什么 $params 从配置文件中获取值而不是设置为默认值?
【发布时间】:2016-08-09 02:54:44
【问题描述】:

我有一段代码,它是 CI 3.1 自定义库的一部分:

class NavigationMenu
{
    protected $CI;

    public function __construct($params = ['config' => 'navigation'])
    {

        // this is where I read $params as an array of 10 values
        // and it shouldn't be since $params has only one key = config
        var_dump($params);

        $this->CI =& get_instance();
        $this->CI->load->helper('url');  
        $this->CI->config->load($params['config'], true);
        $this->CI->load->model('nav_model', 'nav');
    }
    ....
}

文件'navigation.php`的代码如下:

$config['navigation_open']          = '<ul class="nav">';
$config['navigation_close']         = '</ul>';
$config['item_open']                = '<li>';
$config['item_open_active_class']   = 'active';

我注意到$params 传递给构造函数的内容如下:

array (size=10)
  'navigation_open' => string '<ul class="nav">' (length=16)
  'navigation_close' => string '</ul>' (length=5)
  'item_open' => string '<li>' (length=4)
  'item_open_active_class' => string 'active' (length=6)

为什么不读成?

array (size=1)
  'config' => string 'navigation' (length=10)

编辑

我没有使用库,这意味着不是正在创建对象,而是自动加载库,简单如下:

config/autoload.php
$autoload['config'] = ['navigation'];

这将导致在我点击应用程序的 index.php 文件时调用类构造函数。

这是 CI 的默认行为吗?从 PHP 构造函数,我不知道吗?还是我的代码有问题而我没有看到?

【问题讨论】:

  • 阅读manual
  • @csabinho 真的吗?究竟在哪里?因为我已经阅读了它并且没有发现任何关于这个问题的帮助
  • $this-&gt;config-&gt;load('filename'); Where filename is the name of your config file, without the .php file extension.
  • @csabinho 您在阅读我的代码并理解问题所在吗?我正在尝试仅使用文件名加载文件,正如您在传递给构造函数并默认初始化为 ['config' =&gt; 'navigation'] 的参数 $params 中看到的那样,这不起作用,这就是问题
  • params的值是4个元素的数组还是1个元素的数组?如果是 4 个元素,CI 的工作方式与手册中的说明相同。

标签: php codeigniter codeigniter-3 php-5.6


【解决方案1】:

您应该改用$this-&gt;CI-&gt;config-&gt;set_item($params['config'], true)。请仔细阅读this documentation

【讨论】:

  • 这也不起作用,试试我的代码示例,你会看到。我不是想为配置设置一个值我想从构造函数参数加载默认配置文件
  • @ReynierPM 某些东西会覆盖您的默认函数参数,将其放在函数体中。
猜你喜欢
  • 2019-09-20
  • 2013-01-01
  • 2018-11-10
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
相关资源
最近更新 更多