【问题标题】:Laravel helper files not working?Laravel 帮助文件不起作用?
【发布时间】:2018-08-06 21:12:18
【问题描述】:

似乎我的帮助文件 app/helpers.php 根本不起作用。 当我尝试调用其中的函数时,我只是得到一个Call to undefined function App\Http\Controllers\test() 错误,即使它与另一个有效的项目具有完全相同的设置(我认为?)。

似乎可以帮助其他人解决此问题的所有方法对我都不起作用。即,将文件添加到composer.json,转储自动加载等。

帮助文件内容:

<?php

function test()
{
    dd(":(");
}

从控制器调用函数:

public function test()
{
    test();
}

【问题讨论】:

  • 使用 Helpers::test(); 并在您的帮助文件中将函数设为静态 public static function test(){。在控制器上方写上use Helpers;
  • @Bugfixer 问题是,它不是一个类/模型。帮助文件应该(并且已经)在不使其成为类的情况下工作。 :(
  • 如果帮助文件没有声明命名空间,它应该在 composer autoload 部分。
  • 你能分享composer.json文件中你包含帮助文件的部分吗?
  • @JithinJose 帮助文件位于默认命名空间 App 中。但是我还是尝试将命名空间设置为 App 但它也不起作用。 ://

标签: php laravel helpers


【解决方案1】:

您需要load the file with custom helpers。例如,如果它的名字是helper并且它在app目录中:

"autoload": {
    ....
    "files": [
        "app/helper.php"
    ]
},

另外,在此之后运行composer du 命令。

【讨论】:

  • 是的,好几次。
  • 这对我有用,@Slimez 确认您已命名帮助文件并正确链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多