PHP Dotenv导致wordpress配置运行两次

时间:2018-02-19 12:15:59

标签: php wordpress laravel composer-php laravel-dotenv

我有一个相对复杂的laravel项目,在其中我有一个wordpress安装用于与作曲家一起安装的博客。

在我的wp-config.php中,我使用include来命名application.php的配置目录中的文件(用于组织目的)。我在文件中有define('XXX', 'config stuff');和类似的各种情况。当'config stuff'被硬编码时,网站运行得很好,但是最近我正在尝试使用composer安装的dotenv从我的.env中获取值与getenv()

require_once(LARAVEL_PATH . '/vendor/autoload.php');
$dotenv = new Dotenv\Dotenv(APP_ROOT_DIR);
$dotenv->load();

当我var_dump我的getenv('example_env_constant')时,它给了我正确的值绝对正确。所以我在application.php文件中设置了这些。

但是现在当我加载网站时,我得到了大量的

Notice: Constant XXX_XXXX_XXX already defined in /path/to/application.php on line X

每一个define('XXX', value);

一个

还有一个

Cannot modify header information

在测试中,我发现我的wp-config.php文件正在运行一次。但不知怎的,我的application.php正在运行两次。第一次运行来自wp-config.php中的包含调用。触发错误的第二次运行发生在第326行wp-settings.php

do_action( 'plugins_loaded' );

我不知道这是怎么回事。

如果我从application.php删除了dotenv代码,而是直接将其放入我的wp-config.php,则行为完全相同,wp-config.php运行一次,application.php运行两次

现在,如果我删除application.php并将所有代码放入wp-config.php,因为wp配置传统上已完成。然后我再次得到完全相同的问题并且它引用了已删除的文件...这当然表明此时存在缓存问题,尽管我不认为缓存是原始问题的原因。使用wp cli运行缓存刷新不起作用,因为它实际上设法在刷新时从application.php发出相同的错误。不管怎样,首先禁用缓存。这不是一个浏览器缓存问题,无论是作为新的隐身镀铬实例还是硬刷新都没有区别。

这是一个很长的阅读,对此抱歉,我希望我很清楚。我很困惑这是如何发生的,任何有关调试的帮助或提示都会很棒。也许我错过了一些非常明显的东西,因为看起来使用dotenv为我的wp配置彻底打破了我以前从未见过的方式。最糟糕的是我会回到硬编码wp-config文件

更新

我错误地删除了application.php文件而没有停止相关的错误。它会阻止需求错误,但我得到其他人。如果我只是删除application.php的内容,那么我没有区别。

有些事情是非常错误的,如果有人提出任何调试建议,我们将非常感激

文件:

  • 公共/帮助-建议/ wp-config.php

  • 配置/ application.php

  • wordpress安装在public / help-advice / wp

  • 非作曲家生成的wp文件位于public / help-advice / app或 公共/求助咨询

如果您不能使用pastebin粘贴代码:

这是config.php

<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the
 * installation. You don't have to use the web site, you can
 * copy this file to "wp-config.php" and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * MySQL settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://codex.wordpress.org/Editing_wp-config.php
 *
 * @package WordPress
 */

 /*
  * Caching
  */
define('WP_CACHE', true);
define('LARAVEL_PATH', dirname(__FILE__) . '/../..'); // Make sure this is pointed to same server

require_once(LARAVEL_PATH . '/vendor/autoload.php');
require_once(LARAVEL_PATH . '/config/application.php');
require_once(ABSPATH . 'wp-settings.php');

这是application.php

<?php

//This file pulls in data for WP and is included in the wp-config.php file within help-advice

/*
 * Base paths
 */
define('APP_ROOT_DIR', dirname(__DIR__));

// $dotenv = new Dotenv\Dotenv(APP_ROOT_DIR);
// $dotenv->load();
// this one above works but causes this file to run twice causing errors, the one below errors
// if (file_exists(APP_ROOT_DIR . '/.env')) {
//     Dotenv\Dotenv::load(APP_ROOT_DIR);
// }

define('APP_PUBLIC_DIR', APP_ROOT_DIR . '/public/help-advice');
define('APP_STORAGE_DIR', APP_ROOT_DIR . '/storage');
define('APP_LOG_DIR', APP_STORAGE_DIR . '/logs');


// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'redacted');

/** MySQL database username */
define('DB_USER', 'redacted');

/** MySQL database password */
define('DB_PASSWORD', 'redacted');

/** MySQL hostname */
define('DB_HOST', '127.0.0.1');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8mb4');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'redacted');
define('SECURE_AUTH_KEY',  'redacted');
define('LOGGED_IN_KEY',    'redacted');
define('NONCE_KEY',        'redacted');
define('AUTH_SALT',        'redacted');
define('SECURE_AUTH_SALT', 'redacted');
define('LOGGED_IN_SALT',   'redacted');
define('NONCE_SALT',       'redacted');


/*
 * Debugging/errors
 */
define('APP_DEBUG', (boolean) getenv('APP_DEBUG'));
// Always log errors
ini_set('log_errors', 1);
ini_set('error_log', APP_LOG_DIR . '/wp_debug.log');
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', APP_DEBUG);
define('SCRIPT_DEBUG', APP_DEBUG);
/*
 * URLs
 */
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'].'/help-advice/wp');
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'].'/help-advice');
/*
 * Custom Content Directory (/public/help-advice/app)
 */
define('CONTENT_DIR', '/app');
define('WP_CONTENT_DIR', APP_PUBLIC_DIR . CONTENT_DIR);
define('WP_CONTENT_URL', WP_HOME . CONTENT_DIR);


//google analytics
define('GA_PROPERTY_ID',getenv('GA_PROPERTY_ID'));
/**#@-*/

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/../public/help-advice/wp/');

1 个答案:

答案 0 :(得分:0)

我不知道为什么它会运行两次或者是由do_action('plugins_loaded')触发的;在第二轮。

由于这是一个相当独特的构建/情况,我不希望这很容易修复,或者超过一些人需要这个。所以我没有继续撕掉我的头发,而是在我的application.php中为每个定义添加了一个检查,看看它是否已经存在,如果不重新定义的话。例如:if (!defined('WP_DEBUG')) {define('WP_DEBUG', true);}

由于dotenv被添加到application.php,我也得到了来自advanced-cache.php的错误,不知道为什么,特别是因为我从未安装或与高级缓存有任何关系。所以我只是禁用了这个define('WP_CACHE', false);