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

Can't read config.ini file when using phalcon dev tools

This is my config.ini file:

[database]
adapter     = Mysql
host        = localhost
username    = root
password    = 1234
dbname        = my_db

[application]
controllersDir  = ${PAPI_HOME_DIR}/app/controllers/
modelsDir       = ${PAPI_HOME_DIR}/app/models/
viewsDir        = ${PAPI_HOME_DIR}/app/views/
pluginsDir      = ${PAPI_HOME_DIR}/app/plugins/
libraryDir      = ${PAPI_HOME_DIR}/app/library/
cacheDir        = ${PAPI_HOME_DIR}/app/cache/
baseUri         = /

When I try to create a new model I got this error: PHP Warning: file_put_contents(/app/models/Ticker.php): failed to open stream: No such file or directory in /home/dima/Documents/phalcon-devtools/scripts/Phalcon/Builder/Model.php on line 624

How should I configure the config.ini file for phalcon devtools?



98.9k
edited Jul '14

As far as I know, variables like ${PAPI_HOME_DIR} are not processed in a ini file



19.1k
edited Jul '14

But this is works when I use this file with my phalcon app. I mean it looks like that my Phalcon app is able to parse the file correctly while phalcon dev tools can't... the environment variable is settrd uP for all users...



98.9k

It's strange, because Phalcon use parse_ini_file (https://github.com/phalcon/cphalcon/blob/1.3.3/ext/config/adapter/ini.c#L152) which does not support parsing of variables. https://www.php.net/manual/en/function.parse-ini-file.php

parse_ini_file supports env variables, it's an undocumented feature. I think the problem is that you are using different environments. See this question and the relative answer.



19.1k

I just added those two php rows to phalcon.php file to verify if my $PAPI_HOME_PATH variable is available.

echo getenv("PAPI_HOME_PATH");
die();

and output was as expected: command: phalcon model --name my_table output: /home/dima/PhpstormProjects/papi

I agree with this.

'database' => [
        'adapter'     => getenv('DB_ADAPTER'),
        'host'        => getenv('DB_HOST'),
        'port'        => getenv('DB_PORT'),
        'username'    => getenv('DB_USERNAME'),
        'password'    => getenv('DB_PASSWORD'),
        'dbname'      => getenv('DB_NAME'),
        'charset'     => getenv('DB_CHARSET'),
    ],

Above is my configuration file, i use getenv as .env parser. Works well when run normally from server, but when use with phalcon-dev tools it returns

Error: Invalid database Adapter!
edited Mar '18

I agree with this.

'database' => [
       'adapter'     => getenv('DB_ADAPTER'),
       'host'        => getenv('DB_HOST'),
       'port'        => getenv('DB_PORT'),
       'username'    => getenv('DB_USERNAME'),
       'password'    => getenv('DB_PASSWORD'),
       'dbname'      => getenv('DB_NAME'),
       'charset'     => getenv('DB_CHARSET'),
   ],

Above is my configuration file, i use getenv as .env parser. Works well when run normally from server, but when use with phalcon-dev tools it returns

Error: Invalid database Adapter!

I ran into this same problem while working with Phalcon devtools. However, as @dedalozzo suggested, its a problem with cli using a differnet env from web server. Pretty tricky as these are runtime variables as well and I had to work with other devs without container environment setup. Here is how i solved it, add this bit to config.php just before return config statements.

 <?php
 // check if request came from cli then load env variables
 if (! http_response_code() || defined('STDIN')) { 
    require __DIR__."path/to/vendor/autoload.php"; 
    $dotenv = new \Dotenv\Dotenv(__DIR__.'path/to/.env'); 
    $dotenv->load();
 } 

i hope the helps someone out there. Maybe phalcon devtools can scan for occurence of .env files or a better aproach could be suggested

Thanks @byteworksng - this was very helpful when I ran into the same issue today. Nice simple solution!