我刚开始使用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()返回一个项目数组。
由于
答案 0 :(得分:0)
我认为第一种方法(A)应该没问题,它的代码较少,服务目的很好。
答案 1 :(得分:0)
如果您正在为控制器读取一组配置项,请将它们存储在类成员($this->config
)中,如果您正在读取单个配置项;单独阅读。
答案 2 :(得分:0)
如果您希望从“任何地方”访问网站范围的内容,另一种方法可能是:
Kohana::$config->attach(new Kohana_Config_File('global'));
在bootstrap.php中。然后在application / config目录中创建global.php,例如:
return (array ('MyFirstVar' => 'Is One',
'MySecondVar' => 'Is Two'));
然后当你需要代码时:
Kohana::config ('global.MyFirstVar');
但我认为所有这些都取决于你想要使用它的地方和方式。