Search code examples
phpyii2yii2-basic-app

Invalid argument supplied for foreach() yii-app-basic


I've one module -> users.

->modules
    ->users
        ->controllers
        ->models
        ->views
        ->Users.php

I created one 'config.php' inside 'config' folder of 'users' modules.

->modules
    ->users
        ->config
            ->config.php
        ->controllers
            -> List of Controllers
        ->models
            -> List of models
        ->views
            -> List of Views
        ->Users.php

And, i gave directory path of config.php in init() method of Users.php, as

modules/users/Users.php

<?php

namespace app\modules\users;

class Users extends \yii\base\Module
{
    public $controllerNamespace = 'app\modules\users\controllers';
    public $commonModel = 'app\modules\users\models\Users';

    public function init()
    {
        parent::init();
        \Yii::configure($this,require(__DIR__.'/config/config.php'));
    }
}

But, it is giving error like

PHP Warning – yii\base\ErrorException
"Invalid argument supplied for foreach()".

Screenshot

enter image description here enter image description here enter image description here enter image description here

I am taking reference from Yii2.0 Guide to include a path inside init() method.

Please help me to rectify this issue.

Thanks.


Solution

  • From what I can see, you're passing in the PHP code to your config file, instead of passing in a configuration array

    Instead of this...

    \Yii::configure($this, require(__DIR__.'/config/config.php'));
    

    Try doing this...

    $config = require(__DIR__.'/config/config.php');
    \Yii::configure($this, $config);
    

    In your config.php file you should be returning an array, if you're using the basic-app config file and adding to that then it should be set up like this already