Laravel自定义帮助程序类 - 不能从其他类调用非静态方法


Laravel custom Helper class -- can't call non-static methods from other classes

我想在我的自定义帮助程序类中使用 Config::get(( 方法,但总是收到错误。

在帮助程序.php文件的顶部,我有以下内容:

use 'Illuminate'Config'Repository as Config;

然后,我有一个公共静态函数,我想在其中使用 Config::get(( 方法来获取配置设置。为简单起见,我们假设函数为:

public static function getURL() {
    return Config::get('assets.url');
}

我有一个设置了此 url 变量的资产.php文件。Config::get('assets.url'( 方法在我的网站其他地方工作。

但是当尝试在我的助手.php文件中使用 Config::get 时,我收到此错误:

Non-static method Illuminate'Config'Repository::get() should not be called statically

我显然无法将 Config::get 方法更改为静态方法。我能做什么?

您可以尝试仅导入外观,而不是尝试获取基础类。

use Config;

然后像在课堂上一样使用Config