I wondered how the Rails cookie structure is and checked it.
Continue reading Rails 4: See CookieTag Archives: config
CakePHP3 separate config file for each environment
I’ll write how to separate config file in CakePHP3 version 3.0.9. As for FuelPHP, you can add setting file into app/config/development
, etc.
In the case when creating setting file in CakePHP3 for each environment, I’ll recommend you to create the directory config/development
and put setting files in it, and modify configure loading code.
Modified Code
Add one line with Configure::load
into config/bootstrap.php
. In the following code, it’s forth line. Suppoted that $environment
is set. You should define it somewhere.
1 2 3 4 5 6 7 |
try { Configure::config('default', new PhpConfig()); Configure::load('app', 'default', false); Configure::load($environment.'/app', 'default', true); } catch (\Exception $e) { die($e->getMessage() . "\n"); } |
At the forth line, it will merge setting file config/{$development}/app.php
into config/app.php
.
You can add any other conditional statement like if
or switch
.
Explanation
What does Configure
do?
According to bootstrap.php
, Configure::config
and Configure::load
seems to concern loading setting file. Let’s look at these functions.
Class Configure
is defined at vendor/cakephp/cakephp/src/Core/Configure.php
.
Configure::config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * Add a new engine to Configure. Engines allow you to read configuration * files in various formats/storage locations. CakePHP comes with two built-in engines * PhpConfig and IniConfig. You can also implement your own engine classes in your application. * * To add a new engine to Configure: * * ``` * Configure::config('ini', new IniConfig()); * ``` * * @param string $name The name of the engine being configured. This alias is used later to * read values from a specific engine. * @param ConfigEngineInterface $engine The engine to append. * @return void */ public static function config($name, ConfigEngineInterface $engine) { static::$_engines[$name] = $engine; } |
According to the comment, this method adds an engine. First parameter is the key to identify added engines, seconed one is the instance of the engine. You know, if you set the engine with the same name twice or more times, the setting is overwritten by last one.
bootstrap.php
adds engine new PhpConfig()
as default one, with Configure::config('default', new PhpConfig());
.
Configure::load
The method loads setting with the engine set with Configure::config
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/** * Loads stored configuration information from a resource. You can add * config file resource engines with `Configure::config()`. * * Loaded configuration information will be merged with the current * runtime configuration. You can load configuration files from plugins * by preceding the filename with the plugin name. * * `Configure::load('Users.user', 'default')` * * Would load the 'user' config file using the default config engine. You can load * app config files by giving the name of the resource you want loaded. * * ``` * Configure::load('setup', 'default'); * ``` * * If using `default` config and no engine has been configured for it yet, * one will be automatically created using PhpConfig * * @param string $key name of configuration resource to load. * @param string $config Name of the configured engine to use to read the resource identified by $key. * @param bool $merge if config files should be merged instead of simply overridden * @return mixed false if file not found, void if load successful. * @link http://book.cakephp.org/3.0/en/development/configuration.html#reading-and-writing-configuration-files */ public static function load($key, $config = 'default', $merge = true) { $engine = static::_getEngine($config); if (!$engine) { return false; } $values = $engine->read($key); if ($merge) { $values = Hash::merge(static::$_values, $values); } return static::write($values); } |
First parameter specify the file to be load. When you use /
, it means directory separator.
Second parameter specify the engine to handle settings, which is set with Configure::config
. The identify key of the engine is expressed as $name
in Configure::config
, but it’s $config
in here. On specifying the engine unset, return false
.
Third parameter defines to merge or to overwrite. I’ll set it true
, because common setting in all environments should be written in one file.
So, you can merge setting file with Configure::load(filepath, 'default', true)
. You know I didn’t explain the detail like Hash::merge
.
If load not existing file?
You can know what will occur when you try it. But let’s look at what procedure works. According to bootstrap.php
and Configure::load
, the setting file will be load with read
method in PhpConfig
. PhpConfig
is defined at vendor/cakephp/cakephp/src/Core/Configure.php
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * Read a config file and return its contents. * * Files with `.` in the name will be treated as values in plugins. Instead of * reading from the initialized path, plugin keys will be located using Plugin::path(). * * Setting a `$config` variable is deprecated. Use `return` instead. * * @param string $key The identifier to read from. If the key has a . it will be treated * as a plugin prefix. * @return array Parsed configuration values. * @throws \Cake\Core\Exception\Exception when files don't exist or they don't contain `$config`. * Or when files contain '..' as this could lead to abusive reads. */ public function read($key) { $file = $this->_getFilePath($key, true); $return = include $file; if (is_array($return)) { return $return; } if (!isset($config)) { throw new Exception(sprintf('Config file "%s" did not return an array', $key . '.php')); } return $config; } |
It says error will be occurred. The variable $config
seems a relic on the past
Database available in FuelPHP
I wondered what database is available in FuelPHP, and look into FuelPHP code.
Environment
- FuelPHP 1.8 (GitHub)
Available Database
The following DBMS is theoretically available in FuelPHP. It means that FuelPHP use driver for the following databases.
- Cubrid
- FreeTDS
- Microsoft SQL Server
- Sybase
Firebird- IBM DB2
- IBM Informix Dynamic Server
- MySQL
- Oracle Call Interface
- ODBC v3 (IBM DB2, unixODBC and win32 ODBC)
- PostgreSQL
- SQLite 3, 2
- SQL Azure
- 4D
How to investigate
First, read manual. At the section Database Introduction
, it says Can be mysql, mysqli or pdo
. Namely, FuelPHP can handle mysql specific driver and PDO driver.
Then, read PDO section of PHP document. For above all DBMS, PDO is available.
Well, can FuelPHP handle PDO and connect to DB in real? Now, look at the FuelPHP core code, which handle database connection. There are 3 kind of connection.php
, each of them handle mysql, mysqli, PDO connection. It’ll be clear PDO is available in FuelPHP.
Take your Attention when you use sqlite
You should be careful when you use sqlite. (FuelPHP version 1.7.2)
Primary Key
Migration doesn’t work when you add Primary Key like MySQL. In sqlite3, we can’t add name to Primary Key. 例えば MySQL なら Primary Key
となるところが、 sqlite3 だと id
(id
)Primary Key (
というようになります。id
)
So, we should create plain SQL or change FuelPHP core code. It’s a choice not to use Primary Key, but I don’t recommend.
また通常は migration file を php oil
を使って作成すると 自動で UNSIGNED
の id
が作成されて AUTO_INCREMENT
が設定されますが、 sqlite ではこれが動きません。 sqlite では AUTO_INCREMENT
ではなく AUTOINCREMENT
になります。 いまのところ自動生成のスクリプトでは対応していませんので FuelPHP の core
のコードを変更する必要があります。 そして自動生成される UNSIGNED
は 削除しましょう。 sqlite では UNSIGNED BIT INT
というのが使えますが、 使ったところで INTEGER
と解釈されます。
Blank Charset
sqlite3 では charset の指定方法が他のデータベースとは異なります。 PRAGMA
statement を使います。 (確か sqlite3 では標準文字コードが utf-8 だったと思います。)
If you don’t set charset in db config (config/db.php
), php oil migrate
occurs an error of SQL, CREATE TABLE
, because SQL which contains DEFAULT CHARACTER SET utf8
at last is executed. To prevent that, set empty string to charset, charset => ''
. すると、 DEFAULT
から始まる文字コード指定部分がなくなり、 sqlite3 でも php oil migrate
が動くようになります。
db.php
は次のように書きます。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php return array( 'default' => array( 'type' => 'pdo', 'connection' => array( 'dsn' => 'sqlite:'.APPPATH.'migrations/'.Fuel::$env.'.sqlite3', ), 'table_prefix' => '', 'charset' => '', ), ); |
このようにすると、 環境ごとに別の DB が作成されます。 table_prefix
は php oil r migration
の時には不要ですが、 php oil r migration:down
の時に必要になることがあります。
Take your Attention when you use PostgreSQL
Migration doesn’t work. It is because PostgreSQL SQL syntax is somehow different from MySQL one. I recommend you to use FuelPHP Plugin for PostgreSQL.