Nana Partykar
Nana Partykar

Reputation: 10548

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.

Upvotes: 1

Views: 8542

Answers (3)

vietnux
vietnux

Reputation: 11

you edit file model and delete code:

/**
 * @inheritdoc
 * @return DriverSearch the active query used by this AR class.
 */
public static function find()
{
    return new DriverSearch(get_called_class());
}

Upvotes: 1

Houmam
Houmam

Reputation: 587

The problem comes from the configuration file. The configureation file must returns an array. Make sure that the config file is as follows:

<?php

$config = [
    'name1' => 'value1',
    'name2' => [/* something here */],
];

return $config;

Upvotes: 2

Lynch
Lynch

Reputation: 910

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

Upvotes: 2

Related Questions