We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problem reading configuration file on a controller

Hello,

I'm trying to read the configuration file my application and returns the error:

The argument is not initialized or iterable()
0 [internal function]: Phalcon\Config->__construct(Object(Phalcon\Config))
1 C:\server\www\risumer\app\controllers\AuthController.php(32): Phalcon\Config\Adapter\Php->__construct('C:\\server\\www\\r...')
2 [internal function]: AuthController->loginAction()
3 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(AuthController), 'loginAction', Array)
4 [internal function]: Phalcon\Dispatcher->_dispatch()
5 [internal function]: Phalcon\Dispatcher->dispatch()
6 C:\server\www\risumer\public\index.php(29): Phalcon\Mvc\Application->handle()
7 {main}

The code that generates the error:

$config = new \Phalcon\Config\Adapter\PHP(APP_PATH . '/app/config/config.php');
return $config;

Where is the problem?

The version I use is: Phalcon 2.1.0r

Thank you

Perhaps you have made a mistake in your config.php.

Have you tried with \Phalcon\Config\Adapter\Ini, and to define your config in text based .ini format?

How /app/config/config.php looks like ?

edited May '16

Thanks for answering,

Mi file config.php:

<?php

defined('APP_PATH') || define('APP_PATH', realpath('.'));

return new \Phalcon\Config(array(
    'database' => array(
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'BBDDr',
        'password'    => 'PASS',
        'dbname'      => 'BBDD',
        'charset'     => 'utf8',
    ),
    'application' => array(
        'controllersDir' => APP_PATH . '/app/controllers/',
        'modelsDir'      => APP_PATH . '/app/models/',
        'migrationsDir'  => APP_PATH . '/app/migrations/',
        'viewsDir'       => APP_PATH . '/app/views/',
        'pluginsDir'     => APP_PATH . '/app/plugins/',
        'libraryDir'     => APP_PATH . '/app/library/',
        'cacheDir'       => APP_PATH . '/app/cache/',
        'baseUri'        => '',
    ),
    'security' => array(
        'secretKey'     => 'KEY',
        'serverName'    => 'URL',
    )
));

In the services.php file it works correctly:

$di->setShared('db', function () use ($config) {
    $dbConfig = $config->database->toArray();
    $adapter = $dbConfig['adapter'];
    unset($dbConfig['adapter']);

    $class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;

    return new $class($dbConfig);
});

I will try with the .ini file, but I would like to know the reason why i get this error.

Thanks


Edit:

It works with the config.ini file, but my question is that does not work with PHP file, I do the same thing.

Thi is the code:

$config = new Phalcon\Config\Adapter\Ini(APP_PATH . '/app/config/config.ini');
return $config->security;

Looks like bug, create issue on github maybe ?

You should run stable version stack - 2.0.12 at present. Not 2.1.x yet.

BTW, on which environment do you run Phalcon, and which PHP version?

In this case I am in the development environment and use windows. The latest stable version for this platform is the 2.1.0.RC1. (https://phalcon.io/es/download/windows)

In my case I use PHP 5.6 on nginx. Later I will try to prove in Linux to see if it works on this platform, but I understand that in Windows with the stable version should also work



1.6k

Not sure, but are you double returning the config?

You should use this with your current config.php, because you allready return a config Object in config.php

$di->setShared('config', function () {
    return include \APP_PATH . '/config/config.php';
});
$config = $di->getConfig();

Because currently you are doing this (like described in the error message)

new \Phalcon\Config\Adapter\Php(new \Phalcon\Config(...))