2015-05-21 09:38:18 25593次浏览 1条回答 2 悬赏 10 金钱

学到模块这,按照文档上的,在模块init()方法中添加如下代码:

public function init()
{
    parent::init();
    //加载配置文件
    Yii::configure($this,__DIR__.'/config.php');
}

然后在当前目录下创建一个config.php文件:

return [
    //配置模块默认路由
    'defaultRoute'=>'default'
];

然后通过路由访问模块的时候,报错:

PHP Warning – yii\base\ErrorException

Invalid argument supplied for foreach()

    /**
     * Configures an object with the initial property values.
     * @param object $object the object to be configured
     * @param array $properties the property initial values given in terms of name-value pairs.
     * @return object the object itself
     */
    public static function configure($object, $properties)
    {
        foreach ($properties as $name => $value) {
            $object->$name = $value;
        }
 
        return $object;
    }

是说在configure方法中的,我就是按照文档上的写的,这样怎么修改呢?

最佳答案

  • 性感的农民 发布于 2015-05-21 10:55 举报

    虽然php是弱类型语言,但是也要注意形参和实参数据格式的一致性。你调用configure时第二个参数是个文件路径,而人家需要你提供数组。教科书上的例子也写的很清楚了:

    public function init()
    {
        parent::init();
        // initialize the module with the configuration loaded from config.php
        \Yii::configure($this, require(__DIR__ . '/config.php'));
    }
    
    1 条回复
    回复于 2015-05-21 11:35 回复

    哎呀,谢谢,是我太粗心了

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
wenyudengdeng
经理

wenyudengdeng

注册时间:2015-04-26
最后登录:2015-07-15
在线时长:7小时53分
  • 粉丝4
  • 金钱735
  • 威望30
  • 积分1105

热门问题