How to setup dynamically database file in cakephp 3?


#1

I want to configure database from front-end while cakephp project first loading like as cms(wordpress,joomla).

Your help will be highly appreciated. Thanks in advance. Looking forward from anyone who expert in this issue.


#2

https://book.cakephp.org/3.0/en/orm/database-basics.html#database-configuration

You can define as many connections as you want in your configuration file. You can also define additional connections at runtime using Cake\Datasource\ConnectionManager::config() . An example of that would be:

use Cake\Datasource\ConnectionManager; ConnectionManager::config(‘default’, [ ‘className’ => ‘Cake\Database\Connection’, ‘driver’ => ‘Cake\Database\Driver\Mysql’, ‘persistent’ => false, ‘host’ => ‘localhost’, ‘username’ => ‘my_app’, ‘password’ => ‘sekret’, ‘database’ => ‘my_app’, ‘encoding’ => ‘utf8mb4’, ‘timezone’ => ‘UTC’, ‘cacheMetadata’ => true, ]);

Configuration options can also be provided as a DSN string. This is useful when working with environment variables or PaaS providers:

ConnectionManager::config(‘default’, [ ‘url’ => ‘mysql://my_app:sekret@localhost/my_app?encoding=utf8&timezone=UTC&cacheMetadata=true’, ]);

When using a DSN string you can define any additional parameters/options as query string arguments.