【问题标题】:How to call only one item from array with $this->config->item CODEIGNITER?如何使用 $this->config->item CODEIGNITER 仅调用数组中的一项?
【发布时间】:2013-08-06 16:45:03
【问题描述】:

我的配置目录中有一个默认配置文件(default.php)。我已经用自动加载文件加载了它:

$autoload['config'] = array('default');

有我的default.php:

<?php

//$config['secretUserHashKey'] = 'g81h6JH18kASPkgAW16jS132sa186h1';
//$config['secretPhotoNameHashKey'] = 'ghOH3Hs841s98sssp1AHDWPfMHAjd';

// Pages

$config['pagesUrl'] = array(
    'home' => 'homepage',
    'login' => 'loginsite'
);

以及我应该如何在我的视图中使用它?

<li><a href="<?=base_url($this->config->item('home'));?>">Home</a> <span class="divider">/</span></li>

通过这个例子,如果我的字符串不是数组,我可以得到值:

$config['home'] = 'homepage';

那么我应该怎么做才能从我的数组中获取项目?

【问题讨论】:

    标签: codeigniter config


    【解决方案1】:

    试试这个:

    <?php echo $this->config->item('home', 'pagesUrl');?>
    

    【讨论】:

      【解决方案2】:

      我更喜欢这种方式:

      $api_config = ci()->config->item('api');
      if (isset($api_config['api_server']))
      ...
      

      或者在你的情况下:

      $pagesUrl_config = ci()->config->item('pagesUrl');
      if (isset(pagesUrl['home']))
      ...
      

      或在视图中:

      <?= @base_url(@ci()->config->item('pagesUrl')['home'])?>
      

      【讨论】:

      • ci() 是一个自定义函数,它只是 get_instance() 的别名/映射器,但也定义了返回类型 (MY_Controller),这对于语法完成等非常有用。
      【解决方案3】:

      这应该可以解决问题

       $this->config->config['pagesUrl']['home']
      

      【讨论】:

        【解决方案4】:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-11-20
          • 1970-01-01
          • 2012-02-27
          • 2018-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多