6

Seems like my helper file app/helpers.php isn't working at all. I just get a Call to undefined function App\Http\Controllers\test() error when I try to call functions in it even though it has the exact same setup (i think?) as another project that works.

All the things that seemed to help everyone else with this problem doesn't work for me. Ie, adding the file to the composer.json, dumping autoload and so on.

Content of helper file:

<?php

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

Calling the function from a Controller:

public function test()
{
    test();
}
Slimez
  • 557
  • 1
  • 7
  • 21
  • use `Helpers::test();` and make function static `public static function test(){` in your helpers file. write `use Helpers;` above your controller – Bugfixer Feb 27 '18 at 09:16
  • @Bugfixer The thing is, it's not a class/model. The helper file should (and has) worked without making it a class. :( – Slimez Feb 27 '18 at 09:21
  • If there is no namspace declared on helper file, it should be in composer autoload section. – Jithin Jose Feb 27 '18 at 09:26
  • can you share the section of `composer.json` file where you have included the helpers file? – linktoahref Feb 27 '18 at 09:34
  • @JithinJose The helpers file is located in App which is the default namespace. But I tried setting the namespace to App anyway but it didn't work either. :/ – Slimez Feb 27 '18 at 09:36
  • @linktoahref `"autoload": { "psr-4": { "Webmozart\\Assert\\": "src/" }, "files": [ "app/helpers.php" ] }, "autoload-dev": { "psr-4": { "Webmozart\\Assert\\Tests\\": "tests/" }, "files": [ "app/helpers.php" ] }` – Slimez Feb 27 '18 at 09:37
  • @SlimeyStuff Try adding a `return` in your helper method. `function test() { return var_dump('test'); }` – linktoahref Feb 27 '18 at 09:55
  • @linktoahref The thing is that it can't even find the function. Tried it anyway, didn't work. :/ – Slimez Feb 27 '18 at 10:08
  • Did you try calling the function via `\test()`? – Niklas S. Feb 27 '18 at 10:55
  • @MrPixelDream Yes, no success. – Slimez Feb 27 '18 at 11:08

1 Answers1

7

You need to load the file with custom helpers. For example, if its name is helper and it's in the app directory:

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

Also, run the composer dump-autoload command after that.

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279