首页 > 解决方案 > Laravel 5 覆盖辅助函数 __() 因为在 wordpress 中使用

问题描述

我已经阅读了几篇关于stackoverflow的帖子,但没有帮助,所以我希望有人能给出好的答案。

我正在将 Laravel 与 wordpress 一起使用。现在有一个错误。是否可以重命名或其他方法来改变它?

错误:

Fatal error: Cannot redeclare __() (previously declared in C:app\laravel\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:821)

标签: wordpresslaravel-5

解决方案


在 de 文件 wp-blog-header.php 在 wp-load.php 之前加载 l110n.php

if ( !isset($wp_did_header) ) {

    $wp_did_header = true;

    // Load the WordPress library.
    require_once( dirname(__FILE__).'/wp-includes/l10n.php' );
    require_once( dirname(__FILE__) . '/wp-load.php' );

    // Set up the WordPress query.
    wp();

    // Load the theme template.
    require_once( ABSPATH . WPINC . '/template-loader.php' );

}

并在你的 LARAVEL 项目中创建一个自己的帮助文件,使用这个函数并使用自己的名称:

if (! function_exists('ownname')) {
    /**
     * Translate the given message.
     *
     * @param  string  $key
     * @param  array  $replace
     * @param  string  $locale
     * @return \Illuminate\Contracts\Translation\Translator|string
     */
    function ownname($key = null, $replace = [], $locale = null)
    {
        return app('translator')->getFromJson($key, $replace, $locale);
    }
}

推荐阅读