2

Edit For those who may bump into this thread with similar error Remove the namespace from the helpers.php file as it is not a class.

In my own case, the correct code should be

use Illuminate\Http\Request;
use App\Models\User;
use Session;




function showTest(){
    return "test";
}

..and not

namespace App\Helpers;

use Illuminate\Http\Request;
use App\Models\User;
use Session;




function showTest(){
    return "test";
}

Original Question

I am looking to write a global function in laravel using the helper.

Here is my code helpers.php

namespace App\Helpers;

use Illuminate\Http\Request;
use App\Models\User;
use Session;




function showTest(){
    return "test";
}

Composer.json

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },

        "files":[
            "app/Helpers/helpers.php"
        ]
    },

But when i call the function in the controller it returns error;

dd(showTest());

it returns this error;

Call to undefined function App\Http\Controllers\User\showTest()

What could be the problem?

Also I have ran composer dump-autoload

Modified

I have tried all solutions posted here but it’s not working.

Php 8 Laravel 9

3
  • You need the full namespace Commented May 27, 2022 at 11:57
  • @aynber I have included the complete name space or am I missing something? Commented May 27, 2022 at 12:02
  • 1
    Sorry, I should have expanded. You need the full namespace when you call the function, so App\Helpers\showTest() Commented May 27, 2022 at 12:07

5 Answers 5

2

First I want to apologize for all the troubles.

The problem was that I used namespace in my helpers.php file

This thread saved me

https://stackoverflow.com/a/29736097/14940381

Sign up to request clarification or add additional context in comments.

Comments

1

You need to run composer dump-autoload and it will work.

For more details click here

3 Comments

I have done this severally before posting this question as stated.
@Here2Learn Make sure it's dump-autoload, not auto-dump
Yes, sorry for the typo. I did dump-autoload
0

Another way of doing this is to register your Helper files in Laravel's service container.

You could either do this in an existing service provider file, or create a new one, for example HelperServiceProvider.php (can be done with php artisan make:provider)

In the register method of the service provider, you can take your helper file and require them globally, as such

public function register()
{
    require_once(glob(app_path().'/Helpers/helpers.php');
}

If you have multiple files, you can simply put them in a for each

public function register()
{
    foreach (glob(app_path().'/Helpers/*.php') as $filename){
        require_once($filename);
    }
}

1 Comment

Following this method, I got this error Call to undefined function App\Http\Controllers\User\showTest2() I want to die dump ShowTest2() in my controller. Do I need to include anything else in the controller where I want to call it?
0

Try loading them inside AppServiceProvider boot method instead of composer file.

require_once app_path('Helpers/helpers.php');

1 Comment

Return the same error
0

composer.json

"autoload": {
   "psr-4": {
       "App\\": "app/",
       "Database\\Factories\\": "database/factories/",
       "Database\\Seeders\\": "database/seeders/"
   },

   "files":[
       "app/Helpers/helpers.php",
      "app\\Helpers\\helpers.php",
   ]
},

hepler.php

<?php

if(!function_exists('showTest')){
    function showTest()
    {
        return "test";
    }
}

if(!function_exists('showTest2')){
    function showTest2()
    {
        return "test 2";
    }
}

And run composer dump-autoload

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.