M
M
Mikha Pankratov2016-02-08 17:08:25
Yii
Mikha Pankratov, 2016-02-08 17:08:25

How to change the $config array?

Good afternoon,
I need to change the config with different button presses.
For example, there is

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=db',
    'username' => 'root',
    'password' => '12345',
//    'dsn' => 'mysql:host=serser;dbname=db',
//    'username' => 'root',
//    'password' => '54321',
];

Here, when you press the test, one should work, and when you press the button, the non-test should work differently. Tell me how can I do it right? Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mikha Pankratov, 2016-02-08
@frmax

Thanks, sent made using the repository.

N
Nikita, 2016-02-08
@bitver

Let's say there are 4 files main.php, middle.php, main-local-test.php, main-local-prod.php. In main.php grab the contents of middle.php. middle.php is generated by you when you click on the button, which is loaded either from main-local-test.php or from main-local-prod.php.
PS Do not do this, in the sense of buttons, well, don't, there are better solutions and honestly)

D
dimabdc, 2016-02-08
@dimabdc

If you need access to several databases, then create 2 connection configs:
db.php

<?php
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=db',
    'username' => 'root',
    'password' => '12345',
];

db2.php
<?php
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=serser;dbname=db',
    'username' => 'root',
    'password' => '54321',
];

and include them in web.php
'db' => require(__DIR__ . '/db.php'),
'db2' => require(__DIR__ . '/db2.php'),

Accordingly, for a sample from the first base it will be
$posts = Yii::$app-> db ->createCommand('SELECT * FROM post')->queryAll();
, from the second
$posts = Yii::$app-> db2 ->createCommand('SELECT * FROM post')->queryAll();

And if you need different configurations at all, then create 2 configs web.php and web2.php and change them in web/index.php, for example, according to the GET parameter condition

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question