【问题标题】:custom config file error自定义配置文件错误
【发布时间】:2012-07-18 15:49:25
【问题描述】:

我想为站点配置创建一个配置文件。我创建config.php 并将其放入Config 文件夹中。这是它的内容:

<?php
Configure::write('Title', 'My App');
Configure::write('Categories', array(
    'Recipes' => 
        array('url' => '/recipe', 'max' => 10),
    'Foods' => 
        array('url' => '/food', 'max' => 5)
));

并将这段代码放在bootstrap.php的末尾:

Configure::load('config');

但是 cake 返回这个错误:

No variable $config found in ...........\app\Config\config.php.php

Error: An Internal Error Has Occurred.

Stack Trace

    CORE\Cake\Core\Configure.php line 272 → PhpReader->read(string)
    APP\Config\bootstrap.php line 110 → Configure::load(string)
    CORE\Cake\Core\Configure.php line 92 → include(string)
    CORE\Cake\bootstrap.php line 142 → Configure::bootstrap(boolean)
    APP\webroot\index.php line 79 → include(string)

有什么问题??

注意:我使用的是最新版本:2.2.1 稳定版

【问题讨论】:

标签: cakephp


【解决方案1】:

在你的配置文件中你应该使用一个名为$config 的数组变量而不是Configure::write 如果你想使用load() 和默认的PhpReader。见API

例如

<?php
$config = array(
    'Title' => 'MyApp',
    ...
);

http://book.cakephp.org/2.0/en/development/configuration.html#built-in-configuration-readers

【讨论】:

  • 谢谢,如果我想使用Configure::write 需要哪个功能?我应该输入Configure::config('config') 吗?一般来说,在 cake 中拥有应用配置文件的最佳和标准方法是什么?
  • 这就是bootstrap.php 的用途。如果您希望它在一个单独的文件中,可能只在bootstrap.php 中使用include config.php 是最简单的。我不确定其他方式。
  • 完全正确 - 使用数组加载()并使用 Configure::write() 包含/要求
猜你喜欢
  • 1970-01-01
  • 2014-02-14
  • 2010-11-18
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多