0

I am using YII2 framework in netbeans, every time my project is deployed to the web server I have to change the url to that of the web server from localhost. Therefore,
Created a config.php file in the controller folder

define("LOCAL", "http://localhost");
define("WEB", "http://website.com");
global $environment;
$environment= LOCAL; //change to WEB if you're live

Every file that needs it, put this at the top

include_once(dirname( __FILE __)."/config.php");

and every time the url is needed in the code call it using

echo $environment;

But I get the error that $environment is not defined. What am I doing wrong?

Reference How to implement absolute URLs on localhost and web server?

Community
  • 1
  • 1
random_geek
  • 345
  • 2
  • 8
  • 23
  • I think config.php file is not included in project. check any error is displaying or just change the `include_once; ` to `required_once` so that if it is not include, remaining line of code will not run and we can find whether it is included or not. – Gowtham Feb 08 '17 at 06:25

1 Answers1

1

Create db_config.php file in you config folder like below -

define('DB_NAME', '');

define('DB_USER', 'root');

define('DB_PASSWORD', '');

define('DB_HOST', 'localhost');

after that - if you want to access that global variables then user like this - create new function for connection in your controller

private function connectToDb($db_name) {
    include Yii::getAlias('@app') . "/config/db_config.php"; // Include db_config.php file
    $connection = new \yii\db\Connection([
        'dsn' => 'mysql:host=' . DB_HOST . ';dbname=' . $db_name,
        'username' => DB_USER,
        'password' => DB_PASSWORD,
    ]);
    return $connection;
}

After that call above function from anywhere from controller like below -

$super_conn = $this->connectToDb('my_db_name');
$super_conn->open();
$sql = "select * form student where id = 1"; //your query 
$super_conn->createCommand($sql)->execute();

thank you.. hope this will help...

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
Rahul Pawar
  • 1,016
  • 1
  • 8
  • 25