我在files
内的目录custom
中有自定义App
。一个文件名为update.php,因此路径为
App\custom\update.php
在update.php中我有
$config = include __DIR__ . '/../../app/config/database.php';
$servername = $config['connections']['mysql']['host'];
$username = $config['connections']['mysql']['username'];
$password = $config['connections']['mysql']['password'];
$dbname = $config['connections']['mysql']['database'];
其中读取数据库凭据。这完全适用于Laravel 4.2,但在Laravel 5.4中不起作用
对于Laravel 5.4,我已经改变了
的路径$config = include __DIR__ . '/../../../config/database.php';
但错误显示此../../../
而不是执行它
include(/var/www/html/site/app/custom/../../../config/database.php): failed to open stream: No such file or directory
我怎样才能正确地给出路径?
更新:当前代码database.php
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => 'test',
'username' => 'root',
'password' => '123321',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
在update.php中
$config = config_path('database.php');
$servername = config('database.connections.mysql.host');
$username = config('database.connections.mysql.host');
$password = config('database.connections.mysql.host');
$dbname = config('database.connections.mysql.host');
$conn = new mysqli($servername,$username,$password,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
答案 0 :(得分:1)
您不必包含该文件,而是使用辅助方法config()
$servername = config('database.connections.mysql.host');
答案 1 :(得分:0)
如果您真的想要加入它,可以使用config_path()
:
$path = config_path('database.php');