Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用配置文件的正确方法?_Php_Kohana - Fatal编程技术网

Php 使用配置文件的正确方法?

Php 使用配置文件的正确方法?,php,kohana,Php,Kohana,我刚开始使用PHP框架Kohana(V2.3.4),我正在尝试为我的每个控制器设置一个配置文件 我以前从未使用过框架,所以显然Kohana对我来说是新的。我想知道如何设置控制器来读取配置文件 例如,我有一个文章控制器和该控制器的配置文件。我有3种加载配置设置的方法 // config/article.php $config = array( 'display_limit' => 25, // limit of articles to list 'commen

我刚开始使用PHP框架Kohana(V2.3.4),我正在尝试为我的每个控制器设置一个配置文件

我以前从未使用过框架,所以显然Kohana对我来说是新的。我想知道如何设置控制器来读取配置文件

例如,我有一个文章控制器和该控制器的配置文件。我有3种加载配置设置的方法

// config/article.php
$config = array(
    'display_limit'         => 25, // limit of articles to list
    'comment_display_limit' => 20, // limit of comments to list for each article
    // other things
);
我应该吗

A) 将所有内容加载到设置数组中

// set a config array
class article_controller extends controller{

    public $config = array();

    function __construct(){
        $this->config = Kohana::config('article');
    }       
}
B) 加载每个设置并将其设置为自己的属性

// set each config as a property
class article_controller extends controller{

    public $display_limit;
    public $comment_display_limit;

    function __construct(){
        $config = Kohana::config('article');

        foreach ($config as $key => $value){
            $this->$key = $value;
        }
    }
}
C) 仅在需要时加载每个设置

// load config settings only when needed
class article_controller extends controller{

    function __construct(){}

    // list all articles
    function show_all(){
        $display_limit = Kohana::config('article.display_limit');
    }

    // list article, with all comments
    function show($id = 0){
        $comment_display)limit = Kohana::config('article.comment_display_limit');
    }
}
注意:Kohana::config()返回一个项目数组


谢谢

我认为第一种方法(A)应该很好,它的代码更少,并且可以很好地发挥作用。

如果您正在为控制器读取一组配置项,那么将它们存储在类成员(
$this->config
)中,如果您正在读取单个配置项;单独阅读。

如果你想从“任何地方”访问站点范围内的内容,另一种方法可能是放置以下内容:

Kohana::$config->attach(new Kohana_Config_File('global'));
return (array ('MyFirstVar' => 'Is One',
               'MySecondVar' => 'Is Two'));
在bootstrap.php中。然后在application/config目录中创建global.php,如下所示:

Kohana::$config->attach(new Kohana_Config_File('global'));
return (array ('MyFirstVar' => 'Is One',
               'MySecondVar' => 'Is Two'));
然后,当您需要代码中的信息时:

Kohana::config ('global.MyFirstVar');
但我想所有这些都归结到你想在哪里以及如何使用它