简体   繁体   English

YII Framework的本地主机和Web服务器上的绝对URL?

[英]Absolute URL on localhost and web server for YII Framework?

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. 我在netbeans中使用YII2框架,每次将项目部署到Web服务器时,都必须将URL从localhost更改为Web服务器的URL。 Therefore, 因此,
Created a config.php file in the controller folder 在控制器文件夹中创建了一个config.php文件

define("LOCAL", " http://localhost "); define(“ LOCAL”,“ http:// localhost ”);
define("WEB", " http://website.com "); define(“ WEB”,“ http://website.com ”);
global $environment; 全球美元环境;
$environment= LOCAL; $ environment = LOCAL; //change to WEB if you're live //如果还活着,请更改为WEB

Every file that needs it, put this at the top 每个需要它的文件,将其放在顶部

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

and every time the url is needed in the code call it using 每次在代码中需要该网址时,使用

echo $environment; echo $ environment;

But I get the error that $environment is not defined. 但是我得到一个错误,即未定义$ environment。 What am I doing wrong? 我究竟做错了什么?

Reference How to implement absolute URLs on localhost and web server? 参考如何在localhost和Web服务器上实现绝对URL?

Create db_config.php file in you config folder like below - 在您的config文件夹中创建db_config.php文件,如下所示-

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... 谢谢..希望这会有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM