Website transfer from local machine to remote hosting. How to connect the Database (imported via phpmyadmin) to Yii 2 with advanced application template on remote hosting? which files which make changes?

  • Host: test.mysql.ukraine.com.ua
  • Login: test_yii
  • Password: ********

    2 answers 2

    After downloading Yii 2 with advanced application template 2.0.8 from the official site and unpack the archive; in the command line we write init :

    Yii2-init

    After that, the file yii-advanced-app-2.0.8 \ advanced \ common \ config \ main-local.php is created , which contains the database access settings on the local computer:

    <?php return [ 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2advanced', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true, ], ], ]; 

    We connect our existing database by changing the name (for example, y2 ) in the yii-advanced-app-2.0.8 \ advanced \ common \ config \ main-local.php file :

      <?php return [ 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=y2', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true, ], ], ]; 

    and check the connection by clicking on the link (after running the local web server):

    http: //localhost/yii-advanced-app-2.0.8/advanced/frontend/web/index.php

    signup

    signup-OK!

    We load the database to a remote server, look at the access settings in the hosting control panel, and make changes to the yii-advanced-app-2.0.8 \ advanced \ common \ config \ main-local.php file :

      <?php return [ 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=test.mysql.ukraine.com.ua;dbname=test_yii', 'username' => 'test_yii', 'password' => '76dk9fkr', 'charset' => 'utf8', ], 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true, ], ], ]; 

    after which we download all the files from the advanced directory to the remote server.

      In the / config directory there is a db.php file in which the connection settings are written. Here is more detailed.

      You can also make the configuration change automatically when the site loads:

       if (!YII_ENV_TEST) { $config['components']['db']['dsn'] = 'mysql:host=localhost;dbname=default'; $config['components']['db']['username'] = 'root'; $config['components']['db']['password'] = ''; } else { $config['components']['db']['dsn'] = 'mysql:host=production;dbname=default'; $config['components']['db']['username'] = 'root'; $config['components']['db']['password'] = ''; }